发布时间:2019-06-06 14:43:56编辑:Run阅读(7928)
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)-正品低价、品质保障、配送及时、轻松购物!']
47744
46234
37109
34626
29228
25885
24744
19862
19415
17907
5715°
6314°
5834°
5887°
6983°
5829°
5845°
6360°
6315°
7672°