发布时间:2019-06-06 14:43:56编辑:Run阅读(7869)
requests-html async的使用
示例代码:
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)-正品低价、品质保障、配送及时、轻松购物!']
47604
45981
36908
34467
29079
25712
24565
19713
19245
17756
5564°
6155°
5690°
5737°
6703°
5482°
5484°
5988°
5965°
7295°