这里我利用了 script 中热榜数据的列表结构 , 在定位取出相关字符串后 , 先将 js 中的 true 和 false 转化为 Python 中的 True 和 False , 最后直接通过 eval() 来将字符串转化为直接可用的数据列表 。
运行代码结果如图:
![用 Python 监控知乎和微博的热门话题](http://img.jiangsulong.com/220408/1Q1413620-5.jpg)
文章插图
至于对微博热门的解析 , 就是中规中矩地利用 BeautifulSoup 来对网页元素进行定位获取:
import requestsfrom bs4 import BeautifulSoup
url = "https://s.weibo.com/top/summary"headers={"User-Agent":"","Cookie":""}wb_response = requests.get(url,headers=headers)webcontent = wb_response.textsoup = BeautifulSoup(webcontent,"html.parser")index_list = soup.find_all("td",class_="td-01")title_list = soup.find_all("td",class_="td-02")level_list = soup.find_all("td",class_="td-03") topic_list = []for i in range(len(index_list)): item_index = index_list[i].get_text(strip = True) if item_index=="": item_index = "0" item_title = title_list[i].a.get_text(strip = True) if title_list[i].span: item_mark = title_list[i].span.get_text(strip = True) else: item_mark = "置顶" item_level = level_list[i].get_text(strip = True) topic_list.Append({"index":item_index,"title":item_title,"mark":item_mark,"level":item_level,"link":f"https://s.weibo.com/weibo?q=%23{item_title}%23&Refer=top"})print(topic_list)
通过解析 , 将微博热门数据逐条存入列表中:
![用 Python 监控知乎和微博的热门话题](http://img.jiangsulong.com/220408/1Q141C43-6.jpg)
文章插图
后续对拿到的数据加以处理展示 , 即可得到很多有趣的应用或实现某些功能 。例如集成诸多平台排行榜的 “今日热榜”:
![用 Python 监控知乎和微博的热门话题](http://img.jiangsulong.com/220408/1Q1414155-7.jpg)
文章插图
?????今日热榜链接:https://tophub.today因为并未展开爬虫细节 , 今天的总结也比较简单:
- 首先在选取要爬的网址时要给自己降低难度 , 例如同样是知乎热榜 , zhihu.com/hot 需要登陆 , 而 zhihu.com/billboard 无需登录便可访问
- 解析爬取到的内容时 , 要结合具体页面内容选择最便捷的方式 。当需要批量爬取相似页面时 , 也要尽量整理通用的解析策略 。
推荐阅读
- 快速电热水器怎么清洗,洗热水器用什么清洁剂
- 不可拆卸布艺椅子怎么清洗,布艺椅子脏了用什么洗
- 瓷砖缝隙发黑霉用什么清洗剂,瓷砖缝隙周围老发黑擦不出来怎么办
- 大型企业中必用的动态路由OSPF,一分钟了解下基础知识
- hash 算法原理及应用漫谈
- 关键词在SEO优化排名的的运用技巧
- T-POT 19.03.1 安全工具:开源蜜罐部署和使用
- 什么是GUI,Python GUI常用库汇总
- 制作工艺对乌龙茶香气形成作用重大
- 茯苓食用方法有哪些