当前位置:网站首页>pyppeteer爬虫
pyppeteer爬虫
2022-04-23 05:46:00 【圆滚滚的程序员】
import asyncio
import pyppeteer
from user_agents import UA
from collections import namedtuple
Response = namedtuple("rs", "title url html cookies headers history status")
async def get_html(url, timeout=30):
browser = await pyppeteer.launch(headless=True, args=['--no-sandbox'])
page = await browser.newPage()
await page.setUserAgent(UA)
res = await page.goto(url, options={
'timeout': int(timeout * 1000)})
#在while循环里强行查询某元素进行等待
while not await page.querySelector('.share-box'):
pass
# 滚动到页面底部
await page.evaluate('window.scrollBy(0, window.innerHeight)')
data = await page.content()
title = await page.title()
resp_cookies = await page.cookies()
resp_headers = res.headers
resp_history = None
resp_status = res.status
response = Response(
title=title,
url=url,
html=data,
cookies=resp_cookies,
headers=resp_headers,
history=resp_history,
status=resp_status
)
return response
if __name__ == '__main__':
url_list = [
"http://gxt.hunan.gov.cn//gxt/xxgk_71033/czxx/201005/t20100528_2069234.html",
"http://gxt.hunan.gov.cn//gxt/xxgk_71033/czxx/201005/t20100528_2069221.html",
"http://gxt.hunan.gov.cn//gxt/xxgk_71033/czxx/200811/t20081111_2069210.html"
]
task = (get_html(url) for url in url_list)
loop = asyncio.get_event_loop()
results = loop.run_until_complete(asyncio.gather(*task))
for res in results:
print(res.title)
版权声明
本文为[圆滚滚的程序员]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_39483957/article/details/107998043
边栏推荐
猜你喜欢
解决ArcGIS分区统计显示太多唯一值执行失败
Cf1427c the hard work of paparazzi
Guaba and Computational Geometry
Installation and usage skills of idea
Robocode教程5——Enemy类
Import of data
Explanation of the second I interval of 2020 Niuke summer multi school training camp
Database - sorting data
How SYSTEMd uses / etc / init D script
如何安装jsonpath包
随机推荐
Understanding and installing MySQL
Robocode教程5——Enemy类
Robocode教程8——AdvancedRobot
Definition of C class and method
[leetcode169] most elements
SQL -- data definition
根据SQL语句查询出的结果集,将其封装为json
Failure to deliver XID in Seata distributed transaction project
Calculation (enter the calculation formula to get the result)
C语言实现2048小游戏方向合并逻辑
[leetcode 67] sum of two binary numbers
Usage scenario of copyonwritearraylist
Option的正确打开方式
Feign请求日志统一打印
爬取蝉妈妈数据平台商品数据
Rust 中的 Cell 共享可变指针
程序设计训练
Completely clean up MySQL win
Storing inherited knowledge in cloud computing
安全授信