微博的熱搜榜對于研究大眾的流量有非常大的價值。今天的教程就來說說如何爬取微博的熱搜榜。 熱搜榜的鏈接是:
用瀏覽器瀏覽,發現在不登錄的情況下也可以正常查看,那就簡單多了。使用開發者工具(F12)查看頁面邏輯,并拿到每條熱搜的CSS位置,方法如下:
按照這個方法,拿到這個td標簽的selector是:
pl_top_realtimehot > table > tbody > tr:nth-child(3) > td.td-02 其中nth-child(3)指的是第三個tr標簽,因為這條熱搜是在第三名的位置上,但是我們要爬的是所有熱搜,因此:nth-child(3)可以去掉。還要注意的是 pl_top_realtimehot 是該標簽的id,id前需要加#號,最后變成: #pl_top_realtimehot > table > tbody > tr > td.td-02
你可以自定義你想要爬的信息,這里我需要的信息是:熱搜的鏈接及標題、熱搜的熱度。它們分別對應的CSS選擇器是:
鏈接及標題:#pl_top_realtimehot > table > tbody > tr > td.td-02 > a
熱度:#pl_top_realtimehot > table > tbody > tr > td.td-02 > span
值得注意的是鏈接及標題是在同一個地方,鏈接在a標簽的href屬性里,標題在a的文本中,用beautifulsoup有辦法可以都拿到,請看后文代碼。
現在這些信息的位置我們都知道了,接下來可以開始編寫程序。默認你已經安裝好了python,并能使用cmd的pip,如果沒有的話請見這篇教程:python安裝。需要用到的python的包有:
BeautifulSoup4:
cmd/Terminal 安裝指令:
pip install beautifulsoup4
lxml解析器:
cmd/Terminal 安裝指令:
pip install lxml
lxml是python中的一個包,這個包中包含了將html文本轉成xml對象的工具,可以讓我們定位標簽的位置。而能用來識別xml對象中這些標簽的位置的包就是 Beautifulsoup4.
編寫代碼:
# https://s.weibo.com/top/summary/
import requests
from bs4 import BeautifulSoup
if __name__ == "__main__":
news = []
# 新建數組存放熱搜榜
hot_url = 'https://s.weibo.com/top/summary/'
# 熱搜榜鏈接
r = requests.get(hot_url)
# 向鏈接發送get請求獲得頁面
soup = BeautifulSoup(r.text, 'lxml')
# 解析頁面
urls_titles = soup.select('#pl_top_realtimehot > table > tbody > tr > td.td-02 > a')
hotness = soup.select('#pl_top_realtimehot > table > tbody > tr > td.td-02 > span')
for i in range(len(urls_titles)-1):
hot_news = {}
# 將信息保存到字典中
hot_news['title'] = urls_titles[i+1].get_text()
# get_text()獲得a標簽的文本
hot_news['url'] = "https://s.weibo.com"+urls_titles[i]['href']
# ['href']獲得a標簽的鏈接,并補全前綴
hot_news['hotness'] = hotness[i].get_text()
# 獲得熱度文本
news.append(hot_news)
# 字典追加到數組中
print(news)
代碼說明請看注釋,不過這樣做,我們僅僅是將結果保存到數組中,如下所示,其實不易觀看,我們下面將其保存為csv文件。
Python 熱搜榜爬蟲
import datetime
today = datetime.date.today()
f = open('./熱搜榜-%s.csv'%(today), 'w', encoding='utf-8')
for i in news:
f.write(i['title'] + ',' + i['url'] + ','+ i['hotness'] + 'n')
效果如下,怎么樣,是不是好看很多:
Python 微博熱搜榜爬蟲
完整代碼如下:
# https://s.weibo.com/top/summary/
import requests
from bs4 import BeautifulSoup
if __name__ == "__main__":
news = []
# 新建數組存放熱搜榜
hot_url = 'https://s.weibo.com/top/summary/'
# 熱搜榜鏈接
r = requests.get(hot_url)
# 向鏈接發送get請求獲得頁面
soup = BeautifulSoup(r.text, 'lxml')
# 解析頁面
urls_titles = soup.select('#pl_top_realtimehot > table > tbody > tr > td.td-02 > a')
hotness = soup.select('#pl_top_realtimehot > table > tbody > tr > td.td-02 > span')
for i in range(len(urls_titles)-1):
hot_news = {}
# 將信息保存到字典中
hot_news['title'] = urls_titles[i+1].get_text()
# get_text()獲得a標簽的文本
hot_news['url'] = "https://s.weibo.com"+urls_titles[i]['href']
# ['href']獲得a標簽的鏈接,并補全前綴
hot_news['hotness'] = hotness[i].get_text()
# 獲得熱度文本
news.append(hot_news)
# 字典追加到數組中
print(news)
import datetime
today = datetime.date.today()
f = open('./熱搜榜-%s.csv'%(today), 'w', encoding='utf-8')
for i in news:
f.write(i['title'] + ',' + i['url'] + ','+ i['hotness'] + 'n')
?Python實用寶典 (pythondict.com)
不只是一個寶典
歡迎關注公眾號:Python實用寶典
原文來自Python實用寶典:Python 微博熱搜