社会热点
入门级爬取知乎热榜和微博热门数据的代码和思路
2023-01-14 10:03  浏览:288

本文来自编程教室的一名学员 TED 同学,这是他目前正在参与的项目开发小组中的一部分工作,涉及到一些常用的爬虫方法。今天拿出来跟大家分享一下。

一直也没写过爬虫的代码,一来是接触练习的少,二来也对爬虫心存偏见:老有种做贼偷数据的感觉。

最近在体验过爬虫的高效便捷后,觉得确实有必要多实践一下。其实我本身学爬虫没多久,远没到分享爬虫技术的水平。但公众号平台嘛,又不是课堂,分享点实战经验和思路,相互交流下心得,也是挺不错的。

今天来分享下这两天写的入门级的爬取知乎热榜和微博热门数据的代码和思路。首先明确下爬虫、知乎热榜和微博热门这些概念。

网络爬虫(又称为网页蜘蛛微博热点话题,网络机器人,在FOAF社区中间,更经常的称为网页追逐者),是一种按照一定的规则,自动地抓取万维网信息的程序或者脚本。另外一些不常使用的名字还有蚂蚁、自动索引、模拟程序或者蠕虫。

——百度百科,“网络爬虫”

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

知乎热榜链接:https://www.zhihu.com/billboardhttps://www.zhihu.com/hot

张艺兴超级话题微博微博_微博微话题在哪_微博热点话题

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

portant;overflow-wrap: break-word !important;">微博热门链接:portant;overflow-wrap: break-word !important;">https://s.weibo.com/top/summary

微博微话题在哪_张艺兴超级话题微博微博_微博热点话题

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

1. 爬取网页内容

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

微博微话题在哪_微博热点话题_张艺兴超级话题微博微博

知乎热榜

张艺兴超级话题微博微博_微博热点话题_微博微话题在哪

微博热门

这里有两点要注意:

我们选用的网址链接在未登录状态下也可访问,因此 方法中的参数为空也不影响。但爬虫时更多的情况是需要登陆状态,因此也就要求通过设置不同参数来模拟登陆去进行相关操作。

通过 模块获取的网页内容,对应的是在网站上右键单击,选择“显示网页源代码”后展现的页面。它与我们实际看到的网页内容或者 F12 进入开发者模式中看到的网页 是不同的。前者是网络请求后返回结果,后者是浏览器对页面渲染后结果。

2.解析爬到的内容

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

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

Soup 是一个可以从HTML或XML文件中提取数据的库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式. Soup会帮你节省数小时甚至数天的工作时间。

Soup 4.4.0 文档;

之前讲到爬虫所获取的网页对应的是网页源代码,那么在定位网页中目标时可以结合网页源代码来制定解析策略。

这里提一点特别的,在知乎热榜的网页源代码中,拉到最下方可以看到如下:

微博微话题在哪_张艺兴超级话题微博微博_微博热点话题

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

portant;overflow-wrap: break-word !important;">import requestsportant;overflow-wrap: break-word !important;">import reportant;overflow-wrap: break-word !important;">from bs4 import BeautifulSoupportant;overflow-wrap: break-word !important;">
portant;overflow-wrap: break-word !important;">headers={"User-Agent":"","cookie":""}portant;overflow-wrap: break-word !important;">zh_url = "https://www.zhihu.com/billboard"portant;overflow-wrap: break-word !important;">zh_response = requests.get(zh_url,headers=headers)portant;overflow-wrap: break-word !important;">
portant;overflow-wrap: break-word !important;">webcontent = zh_response.textportant;overflow-wrap: break-word !important;">soup = BeautifulSoup(webcontent,"html.parser")portant;overflow-wrap: break-word !important;">script_text = soup.find("script",id="js-initialData").get_text()portant;overflow-wrap: break-word !important;">rule = r'"hotList":(.*?),"guestFeeds"'portant;overflow-wrap: break-word !important;">result = re.findall(rule,script_text)portant;overflow-wrap: break-word !important;">
portant;overflow-wrap: break-word !important;">temp = result[0].replace("false","False").replace("true","True")portant;overflow-wrap: break-word !important;">hot_list = eval(temp)portant;overflow-wrap: break-word !important;">print(hot_list)

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

运行代码结果如图:

微博微话题在哪_微博热点话题_张艺兴超级话题微博微博

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

portant;overflow-wrap: break-word !important;">import requestsportant;overflow-wrap: break-word !important;">from bs4 import BeautifulSoupportant;overflow-wrap: break-word !important;">
portant;overflow-wrap: break-word !important;">
portant;overflow-wrap: break-word !important;">url = "https://s.weibo.com/top/summary"portant;overflow-wrap: break-word !important;">headers={"User-Agent":"","cookie":""}portant;overflow-wrap: break-word !important;">wb_response = requests.get(url,headers=headers)portant;overflow-wrap: break-word !important;">webcontent = wb_response.textportant;overflow-wrap: break-word !important;">soup = BeautifulSoup(webcontent,"html.parser")portant;overflow-wrap: break-word !important;">index_list = soup.find_all("td",class_="td-01")portant;overflow-wrap: break-word !important;">title_list = soup.find_all("td",class_="td-02")portant;overflow-wrap: break-word !important;">level_list = soup.find_all("td",class_="td-03")portant;overflow-wrap: break-word !important;">
portant;overflow-wrap: break-word !important;">topic_list = []portant;overflow-wrap: break-word !important;">for i in range(len(index_list)):portant;overflow-wrap: break-word !important;"> item_index = index_list[i].get_text(strip = True)portant;overflow-wrap: break-word !important;"> if item_index=="":portant;overflow-wrap: break-word !important;"> item_index = "0"portant;overflow-wrap: break-word !important;"> item_title = title_list[i].a.get_text(strip = True)portant;overflow-wrap: break-word !important;"> if title_list[i].span:portant;overflow-wrap: break-word !important;"> item_mark = title_list[i].span.get_text(strip = True) portant;overflow-wrap: break-word !important;"> else:portant;overflow-wrap: break-word !important;"> item_mark = "置顶"portant;overflow-wrap: break-word !important;"> item_level = level_list[i].get_text(strip = True)portant;overflow-wrap: break-word !important;"> 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"})portant;overflow-wrap: break-word !important;">print(topic_list)

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

微博热点话题_微博微话题在哪_张艺兴超级话题微博微博

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

微博微话题在哪_张艺兴超级话题微博微博_微博热点话题

portant;overflow-wrap: break-word !important;">今日热榜链接:portant;overflow-wrap: break-word !important;">https://tophub.today

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

首先在选取要爬的网址时要给自己降低难度,例如同样是知乎热榜微博热点话题,zhihu.com/hot需要登陆,而zhihu.com/无需登录便可访问

解析爬取到的内容时,要结合具体页面内容选择最便捷的方式。当需要批量爬取相似页面时,也要尽量整理通用的解析策略。

portant;overflow-wrap: break-word !important;">代码已上传 GitHub,链接如下:portant;overflow-wrap: break-word !important;">https://github.com/pengfexue2/hot_display.git

当然,拿到数据只是开始,后续如何去处理才是关键和价值所在,之后我们继续探讨。

以上,感谢阅读!

发表评论
0评