requests-html async的使用
示例代码:
代码语言:javascript复制from requests_html import AsyncHTMLSession
asession = AsyncHTMLSession()
ua = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36"
async def get_jd():
r = await asession.get('https://www.jd.com', headers={'user-agent': ua})
return r.html.xpath("//head/title/text()")[0]
async def get_taobao():
r = await asession.get('https://www.taobao.com', headers={'user-agent': ua})
return r.html.xpath("//head/title/text()")[0]
async def get_amazon():
r = await asession.get('https://www.amazon.cn', headers={'user-agent': ua})
return r.html.xpath("//title/text()")[0]
async def get_suning():
r = await asession.get('https://www.suning.com', headers={'user-agent': ua})
return r.html.xpath("//title/text()")[0]
results = asession.run(get_jd, get_taobao, get_amazon, get_suning)
print(results)
返回结果;
['苏宁易购(Suning.com)-送货更准时、价格更超值、上新货更快', '亚马逊-网上购物商城:要网购, 就来Z.cn!', '淘宝网 - 淘!我喜欢', '京东(JD.COM)-正品低价、品质保障、配送及时、轻松购物!']