Python爬虫 爬取新浪微博热搜
生活随笔
收集整理的這篇文章主要介紹了
Python爬虫 爬取新浪微博热搜
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Python爬蟲 爬取新浪微博熱搜
文章目錄
- Python爬蟲 爬取新浪微博熱搜
- 網頁分析
- 數據爬取
- 數據存儲
- 全部代碼
網頁分析
找到熱搜的排名,標題和熱度,發現它們在同一路徑
數據爬取
import requests from lxml import etree url= 'https://s.weibo.com/top/summary?Refer=top_hot&topnav=1&wvr=6' #print(response.text) headers={'User-Agent': 'Mozilla/5.0 (Wind ows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36' } response=requests.get(url,headers=headers) html=etree.HTML(response.text) datas=html.xpath('//*[@id="pl_top_realtimehot"]/table/tbody/tr') for data in datas:data_title=data.xpath('td[2]/a/text()')#標題的xpath路徑print(data_title)運行結果
數據存儲
fp=open('D:/老齊/微博.txt', 'a+')#a+,如果文件不存在就創建,存在就在內容后追加 for data in datas:data_title=''.join(data.xpath('td[2]/a/text()'))#標題data_rank=''.join(data.xpath('td[1]/text()'))#排名data_num=''.join(data.xpath('td[2]/span/text()'))print(data_rank,data_title,data_num,file=fp) fp.close()全部代碼
import requests from lxml import etree url= 'https://s.weibo.com/top/summary?Refer=top_hot&topnav=1&wvr=6' #print(response.text) headers={'User-Agent': 'Mozilla/5.0 (Wind ows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.104 Safari/537.36' } response=requests.get(url,headers=headers) html=etree.HTML(response.text) datas=html.xpath('//*[@id="pl_top_realtimehot"]/table/tbody/tr') fp=open('D:/老齊/微博.txt', 'a+')#a+,如果文件不存在就創建,存在就在內容后追加 for data in datas:data_title=''.join(data.xpath('td[2]/a/text()'))#標題data_rank=''.join(data.xpath('td[1]/text()'))#排名data_num=''.join(data.xpath('td[2]/span/text()'))print(data_rank,data_title,data_num,file=fp) fp.close()總結
以上是生活随笔為你收集整理的Python爬虫 爬取新浪微博热搜的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ABR算法研究综述 | A Survey
- 下一篇: tinyproxy的使用