用 Python 监控知乎和微博的热门话题

前言

文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理。

作者: TED Crossin的编程教室

PS:如有需要Python学习资料的小伙伴可以加点击下方链接自行获取

http://note.youdao.com/noteshare?id=3054cce4add8a909e784ad934f956cef

知乎热榜中的内容热度值,是根据该条内容近24小时内的浏览量、互动量、专业加权、创作时间及在榜时间等维度,综合计算得出的。知乎热榜即根据内容热度值制定的排行榜。

《用 Python 监控知乎和微博的热门话题》

微博的热度值是根据该篇微博被转发、点赞数和微博发布时间等各项因素,来算出热度基数,再与热度权重相加,得出最终的热度值。微博热门即话题热度排行榜。

《用 Python 监控知乎和微博的热门话题》

今天我们要做的就是将相关排行榜中的话题内容爬取下来当作数据素材。换句话说,我们要把页面上排好的信息,通过代码读取并保存起来。

1. 爬取网页内容

Python 爬虫通常采用 requests 库来处理网络请求。这里关于 requests 的方法和参数暂不展开。

知乎热榜

《用 Python 监控知乎和微博的热门话题》

微博热门

《用 Python 监控知乎和微博的热门话题》

这里有两点要注意:

1、我们选用的网址链接在未登录状态下也可访问,因此 requests 方法中的参数为空也不影响。但爬虫时更多的情况是需要登陆状态,因此也就要求通过设置不同参数来模拟登陆去进行相关操作。 2、通过 requests 模块获取的网页内容,对应的是在网站上右键单击,选择“显示网页源代码”后展现的页面。它与我们实际看到的网页内容或者 F12 进入开发者模式中看到的网页 elements 是不同的。前者是网络请求后返回结果,后者是浏览器对页面渲染后结果。

2. 解析爬到的内容

第一步爬到的是整个页面内容,接下来要在所有内容中去对目标定位,然后将其读取并保存起来。

这里我采用的是 BeautifulSoup,因为学爬虫最先接触这个,用起来也蛮顺手。通过 BeautifulSoup 提供的方法和参数,可以很便捷定位到目标。

在知乎热榜的网页源代码中,拉到最下方可以看到如下:

《用 Python 监控知乎和微博的热门话题》

在源代码中网页的 script 部分,有现成的整理好的热榜数据。所以我们为了减少工作量,直接通过 BeautifulSoup 取出 script 中内容,再用正则表达式匹配热榜数据列表处的内容。

 1 import requests
 2 import re
 3 from bs4 import BeautifulSoup
 4  5 headers={"User-Agent":"","Cookie":""}
 6 zh_url = "https://www.zhihu.com/billboard"
 7 zh_response = requests.get(zh_url,headers=headers)
 8  9 webcontent = zh_response.text
10 soup = BeautifulSoup(webcontent,"html.parser")
11 script_text = soup.find("script",id="js-initialData").get_text()
12 rule = r'"hotList":(.*?),"guestFeeds"'
13 result = re.findall(rule,script_text)
14 15 temp = result[0].replace("false","False").replace("true","True")
16 hot_list = eval(temp)
17 print(hot_list)

 

这里我利用了 script 中热榜数据的列表结构,在定位取出相关字符串后,先将 js 中的 true 和 false 转化为 Python 中的 True 和 False,最后直接通过 eval() 来将字符串转化为直接可用的数据列表。

运行代码结果如图:

《用 Python 监控知乎和微博的热门话题》

至于对微博热门的解析,就是中规中矩地利用 BeautifulSoup 来对网页元素进行定位获取:

 1 import requests
 2 from bs4 import BeautifulSoup
 3  4  5 url = "https://s.weibo.com/top/summary"
 6 headers={"User-Agent":"","Cookie":""}
 7 wb_response = requests.get(url,headers=headers)
 8 webcontent = wb_response.text
 9 soup = BeautifulSoup(webcontent,"html.parser")
10 index_list = soup.find_all("td",class_="td-01")
11 title_list = soup.find_all("td",class_="td-02")
12 level_list = soup.find_all("td",class_="td-03")
13 14 topic_list = []
15 for i in range(len(index_list)):
16     item_index = index_list[i].get_text(strip = True)
17     if item_index=="":
18         item_index = "0"
19     item_title = title_list[i].a.get_text(strip = True)
20     if title_list[i].span:
21         item_mark = title_list[i].span.get_text(strip = True)        
22     else:
23         item_mark = "置顶"
24     item_level = level_list[i].get_text(strip = True)
25     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"})
26 print(topic_list)

 

通过解析,将微博热门数据逐条存入列表中:

《用 Python 监控知乎和微博的热门话题》

后续对拿到的数据加以处理展示,即可得到很多有趣的应用或实现某些功能。例如集成诸多平台排行榜的 “今日热榜”:

《用 Python 监控知乎和微博的热门话题》

因为并未展开爬虫细节,今天的总结也比较简单:

1、首先在选取要爬的网址时要给自己降低难度,例如同样是知乎热榜,zhihu.com/hot 需要登陆,而 zhihu.com/billboard 无需登录便可访问 2、解析爬取到的内容时,要结合具体页面内容选择最便捷的方式。当需要批量爬取相似页面时,也要尽量整理通用的解析策略。

完整代码

weibo_top.py

 1 import requests
 2 from bs4 import BeautifulSoup
 3  4 url = "https://s.weibo.com/top/summary"
 5 headers = {"User-Agent": "", "Cookie": ""}
 6 wb_response = requests.get(url, headers=headers)
 7 webcontent = wb_response.text
 8 soup = BeautifulSoup(webcontent, "html.parser")
 9 index_list = soup.find_all("td", class_="td-01")
10 title_list = soup.find_all("td", class_="td-02")
11 level_list = soup.find_all("td", class_="td-03")
12 13 topic_list = []
14 for i in range(len(index_list)):
15     item_index = index_list[i].get_text(strip=True)
16     if item_index == "":
17         item_index = "0"
18     item_title = title_list[i].a.get_text(strip=True)
19     if title_list[i].span:
20         item_mark = title_list[i].span.get_text(strip=True)
21 22     else:
23         item_mark = "置顶"
24     item_level = level_list[i].get_text(strip=True)
25     topic_list.append({"index": item_index, "title": item_title, "mark": item_mark, "level": item_level,
26                        "link": f"https://s.weibo.com/weibo?q=%23{item_title}%23&Refer=top"})
27 print(topic_list)

 

zhihu_billboard.py

 1 import requests
 2 import re
 3 from bs4 import BeautifulSoup
 4  5 headers={"User-Agent":"","Cookie":""}
 6 zh_url = "https://www.zhihu.com/billboard"
 7 zh_response = requests.get(zh_url,headers=headers)
 8  9 webcontent = zh_response.text
10 soup = BeautifulSoup(webcontent,"html.parser")
11 script_text = soup.find("script",id="js-initialData").get_text()
12 rule = r'"hotList":(.*?),"guestFeeds"'
13 result = re.findall(rule,script_text)
14 15 temp = result[0].replace("false","False").replace("true","True")
16 hot_list = eval(temp)
17 print(hot_list)

 

    原文作者:有趣的Python
    原文地址: https://www.cnblogs.com/Qqun821460695/p/11935054.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞