其他分享
首页 > 其他分享> > 爬取新浪微博热搜排行

爬取新浪微博热搜排行

作者:互联网

爬取新浪微博热搜排行

1.1 爬虫基本原理解析

1.2 网页源码获取及转换

1.3 节点、属性、内容的获取

1.4 爬取新浪微博热搜思路梳理

from lxml import etree
import requests
from lxml.html import tostring

res = requests.get("https://s.weibo.com/top/summary?Refer=top_hot&topnav=1&wvr=6")
# 数据解析 -- 把网页变成xpath 结构
html = etree.HTML(res.text)
# 排行
top = html.xpath('//td[@class="td-01 ranktop"]/text()')
# 热搜内容
content = html.xpath('//td[@class="td-02"]/a/text()')
# 热搜数
hot = html.xpath('//td[@class="td-02"]/span/text()')
# print(top)
# print(content)
# print(hot)
print(len(top), len(content), len(hot))
# 内容多一条 -- (热搜置顶)
for i in range(len(hot)):
    print(top[i], content[i + 1], hot[i])

标签:xpath,text,top,爬虫,爬取,print,html,排行,微博热
来源: https://blog.csdn.net/weixin_46984154/article/details/112777367