Python异步爬取知乎热榜实例分享
作者:程序员班长 发布时间:2022-02-26 04:48:47
标签:Python,异步,爬取,知乎,热榜
一、错误代码:摘要和详细的url获取不到
import asyncio
from bs4 import BeautifulSoup
import aiohttp
headers={
'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36',
'referer': 'https://www.baidu.com/s?tn=02003390_43_hao_pg&isource=infinity&iname=baidu&itype=web&ie=utf-8&wd=%E7%9F%A5%E4%B9%8E%E7%83%AD%E6%A6%9C'
}
async def getPages(url):
async with aiohttp.ClientSession(headers=headers) as session:
async with session.get(url) as resp:
print(resp.status) # 打印状态码
html=await resp.text()
soup=BeautifulSoup(html,'lxml')
items=soup.select('.HotList-item')
for item in items:
title=item.select('.HotList-itemTitle')[0].text
try:
abstract=item.select('.HotList-itemExcerpt')[0].text
except:
abstract='No Abstract'
hot=item.select('.HotList-itemMetrics')[0].text
try:
img=item.select('.HotList-itemImgContainer img')['src']
except:
img='No Img'
print("{}\n{}\n{}".format(title,abstract,img))
if __name__ == '__main__':
url='https://www.zhihu.com/billboard'
loop=asyncio.get_event_loop()
loop.run_until_complete(getPages(url))
loop.close()
二、查看JS代码
发现详细链接、图片链接、问题摘要等都在JS里面(CSDN的开发者助手插件确实好用)
正则表达式获取上述信息:
接下来就是详细的代码啦
import asyncio
import json
import re
import aiohttp
headers={
'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36',
'referer': 'https://www.baidu.com/s?tn=02003390_43_hao_pg&isource=infinity&iname=baidu&itype=web&ie=utf-8&wd=%E7%9F%A5%E4%B9%8E%E7%83%AD%E6%A6%9C'
}
async def getPages(url):
async with aiohttp.ClientSession(headers=headers) as session:
async with session.get(url) as resp:
print(resp.status) # 打印状态码
html=await resp.text()
regex=re.compile('"hotList":(.*?),"guestFeeds":')
text=regex.search(html).group(1)
# print(json.loads(text)) # json换成字典格式
for item in json.loads(text):
title=item['target']['titleArea']['text']
question=item['target']['excerptArea']['text']
hot=item['target']['metricsArea']['text']
link=item['target']['link']['url']
img=item['target']['imageArea']['url']
if not img:
img='No Img'
if not question:
question='No Abstract'
print("Title:{}\nPopular:{}\nQuestion:{}\nLink:{}\nImg:{}".format(title,hot,question,link,img))
if __name__ == '__main__':
url='https://www.zhihu.com/billboard'
loop=asyncio.get_event_loop()
loop.run_until_complete(getPages(url))
loop.close()
来源:https://kantlee.blog.csdn.net/article/details/113665084
0
投稿
猜你喜欢
- 终于能出来透口气,写点东西了。前段太忙,也很郁闷,现在调整过来点了。我的设计原则就是在满足需求的前提下,尽可能的简化,简化,再简化。有一次跟
- 在Flash播放器运行时,将不同来源的资源划分到独立的沙箱(sandbox)内,不同沙箱之间不能彼此操作数据(除非目标沙箱做过一些设置,授权
- 一、必备插件🌾Chinese(中文)安装后,按快捷键Ctrl+Shift+P,输入configure languageSettings Sy
- 本期做一个selenium详细实例,会把我在元素定位中遇到的一些阻塞和经验分享给大家。(浏览器为Chrome)(如果只需要最终的完整代码,请
- 热的东西显然会变凉。房间会会人沮丧地变得凌乱。几乎同样,消息会失真。逆转这些情况的短期策略分别是重新加热、 做卫生和使用 Hop
- 本文实例为大家分享了vue动态控制el-table表格列的展示与隐藏的具体代码,供大家参考,具体内容如下1.引入el-table组件,这里我
- 引用是什么在 PHP 中引用意味着用不同的名字访问同一个变量内容。这并不像 C 的指针,替代的是,引用是符号表别名。注意在 PHP 中,变量
- 通常操作系统和软件开发包中都包含文本编辑器,可以用来编辑配置文件,文档文件和源代码。下面是笔者总结的10个最好的免费代码文本编辑器:1.NO
- 众所周知tensorflow造势虽大却很难用,因此推荐使用Keras,它缺省是基于tensorflow的,但通过修改keras.json也可
- 欢迎来到 Python Httpx 教程。在本教程中,我们将深入探讨 Httpx 库,并学习如何使用它来构建高性能的异步网络应用程序。什么是
- 先使用 Ms Access 压缩修复,然后再去相关的表把备注类型的字段里的“索引”去掉 备注型字段为什么不能超过1950字节?是由于备注型字
- 步骤:Settings-->Editor-->Colors & Fonts-->Font然后在size
- 微信小程序 HTTPS报错常见问题及解决方案微信小程序开放公测已经一个多月了,因官方需求文档要求后台使用HTTPS请求进行网络通信,不满足条
- 如今,体育运动的热潮日益流行。同样,以不正确的方式进行运动的风险也在增加。有时可能会导致严重的伤害。考虑到这些原因,提出一种以分析运动员的关
- 目录1.任务要求2.简单设计3.模块实现4.总结由于一些小原因,被迫开始了tkinter一次实战演练。在此做一些记录,总结以及给自己留一些轮
- 一个简单的for语句就能循环字典的所有键,就像处理序列一样:In [1]: d = {'x':1, 'y':
- 前言wx.gird.Gird是实现类似excel表格的库,扩展面很广,本文讲述它添加按钮,按钮响应的内容实现效果图如下:本文基于wxPyth
- 插值主要用于物理学数学中,逼近某一确定值的方法(1)插值是通过已知的离散数据求未知数据的方法。(2)与拟合不同,插值要求曲线通过所有的已知数
- 安装环境:python版本2.7.5 ,win7系统安装Djangohttps://www.djangoproject.com/downlo
- 为网页设置防火墙的主要目的是根据网页内容对不同来访者提供不同的服务,利用Java Script或VB Script,我们很容易做到这一点。但