爬取微博热搜榜
Posted zyy-k
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了爬取微博热搜榜相关的知识,希望对你有一定的参考价值。
import requests
from bs4 import BeautifulSoup
url = ‘https://s.weibo.com/top/summary?cate=realtimehot‘
headers = {‘User-Agent‘:‘Mozilla/5.0 (Windows NT 6.3; Win64; x64)‘
‘AppleWebKit/537.36 (Khtml, like Gecko) Chrome/69.0.3497.100 Safari/537.36‘}
urls = requests.get(url, headers=headers)
urls.encoding = urls.apparent_encoding
text = urls.text
soup = BeautifulSoup(text, ‘lxml‘)
a = soup.find_all(class_="td-02")
b = [i.get_text() for i in a]
print(‘热搜榜‘)
print(‘{:25}‘.format(‘标题(搜索数)‘))
for i,y in zip(b,a):
print(‘{:10} {}
‘.format(i,y))
以上是关于爬取微博热搜榜的主要内容,如果未能解决你的问题,请参考以下文章