python爬虫搜狐新闻_Python爬虫——主题抓取搜狐新闻(步骤及代码实现),爬取...

import requests

from bs4 import BeautifulSoup

import jieba

from gensim.corpora.dictionary import Dictionary

import re

import jieba.analyse as ana

def getdata():

#news_all=[]

news_dictall={}

for p in range(1,10):

p2=1603263206992+p*8

url='https://v2.sohu.com/public-api/feed?scene=CATEGORY&sceneId=1460&page='+str(p)+'&size=20&_='+str(p2)

headers={

'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36',

'cookie':'itssohu=true; BAIDU_SSP_lcr=https://news.hao123.com/wangzhi; IPLOC=CN3300; SUV=201021142102FD7T; reqtype=pc; gidinf=x099980109ee124d51195e802000a3aab2e8ca7bf7da; t=1603261548713; jv=78160d8250d5ed3e3248758eeacbc62e-kuzhE2gk1603261903982; ppinf=2|1603261904|1604471504|bG9naW5pZDowOnx1c2VyaWQ6Mjg6MTMxODgwMjEyODc2ODQzODI3MkBzb2h1LmNvbXxzZXJ2aWNldXNlOjMwOjAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMHxjcnQ6MTA6MjAyMC0xMC0yMXxlbXQ6MTowfGFwcGlkOjY6MTE2MDA1fHRydXN0OjE6MXxwYXJ0bmVyaWQ6MTowfHJlbGF0aW9uOjA6fHV1aWQ6MTY6czExZjVhZTI2NTJiNmM3Nnx1aWQ6MTY6czExZjVhZTI2NTJiNmM3Nnx1bmlxbmFtZTowOnw; pprdig=L2Psu-NwDR2a1BZITLwhlxdvI2OrHzl6jqQlF3zP4z70gqsyYxXmf5dCZGuhPFZ-XWWE5mflwnCHURGUQaB5cxxf8HKpzVIbqTJJ3_TNhPgpDMMQdFo64Cqoay43UxanOZJc4-9dcAE6GU3PIufRjmHw_LApBXLN7sOMUodmfYE; ppmdig=1603261913000000cfdc2813caf37424544d67b1ffee4770'

}

res=requests.get(url,headers=headers)

soup=BeautifulSoup(res.text,'lxml')

news=re.findall('"mobileTitle":"(.*?)",',str(soup))

herf=re.findall('"originalSource":"(.*?)"',str(soup))

#news=soup.find_all("div",attrs={'class':'news-wrapper'})

#html=etree.HTML(res.text)

#news=html.xpath('/html/body/div[2]/div[1]/div[2]/div[2]/div/div[3]/div[3]/h4/a/text()')

news_dic=dict(zip(news,herf))#把标题和链接储存到字典

for k,v in news_dic.items():

news_dictall[k]=v #每一页的字典合并

return(news_dictall)#返回总字典

def ifsim(topicwords):

news_dicfin={}

news_dic=getdata()

ana.set_stop_words('D:\作业\python\文本挖掘\数据集\新闻数据集\data\stopwords.txt') # 输入停用词

for k,v in news_dic.items():

word_list=ana.extract_tags(k,topK=50,withWeight=False) #去除停用词+词频分析

#word_lil.append(word_list)

word_lil=[]

for i in word_list:

word_lil.append([i])#将分词转化为list in list 形式以便传入dictionary

word_dic=Dictionary(word_lil)#转化为dictionary词典形式 以便分析

d=dict(word_dic.items())

docwords=set(d.values())

#相关度计算

commwords=topicwords.intersection(docwords)#取交集

if len(commwords)>0:#交集>0符合条件的存入最终的字典

news_dicfin[k]=v

print(news_dicfin)

if __name__=='__main__':

topicwords={"疫情","新冠","肺炎","确诊","病例"}

ifsim(topicwords)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值