失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > python网络爬虫 爬取新闻标题 时间 内容

python网络爬虫 爬取新闻标题 时间 内容

时间:2020-07-19 13:57:15

相关推荐

python网络爬虫 爬取新闻标题 时间 内容

(爬取清华大学体育部重要新闻前十页)

运行截图:

代码:

import requestsfrom bs4 import BeautifulSoupimport csvimport time# http://www.thsports./publish/sports/1899/index.html# http://www.thsports./publish/sports/1899/index_2.html# http://www.thsports./publish/sports/1899/index_3.htmlurl_1=["http://www.thsports./publish/sports/1899/index_{}.html".format(i) for i in range(2,10)]urls=url_1+["http://www.thsports./publish/sports/1899/index.html"]#print (urls)for url in urls:headers={'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36 Edg/90.0.818.56'}time.sleep(5)r = requests.get(url,headers=headers)r.encoding="utf-8"html= r.text#print(html)with open("qhsport.html","w+",encoding="utf-8") as f:f.write(html)soup = BeautifulSoup(html,"lxml")news=soup.find(class_="box_list").find_all("li")#news=soup.find_all('ul')[2].find_all('li')for new in news:times=new.find(name="p").get_text()title=new.find(name="a").get("title")link=new.find(name="a").get("href")# print(link)url2="http://www.thsports."+link# print(url2)headers={'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36 Edg/90.0.818.56'}con=requests.get(url2,headers=headers)con.encoding="utf-8"html2=con.textsoup2=BeautifulSoup(html2,"lxml")content=soup2.find(class_="box_detail").get_text()print(times,title,url2,content)with open("qh_news.csv","a",newline="",encoding="utf_8_sig")as f:writer=csv.writer(f)writer.writerow((times,title,url2,content))print("爬取完毕!")

如果觉得《python网络爬虫 爬取新闻标题 时间 内容》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。