grequests,并发执行接口请求的方法(简易版)
有时候需要处理很多请求,显然,一个一个去处理是要花费很多时间的
我们就需要用到并发的方式,python并发请求的方法很多,从简单到复杂。
本案例,介绍一个超级简单,使用grequests库,实现并发请求
案例应该是一系列查询的操作,具体忘记了,很久前写的,接口参数狠简单,headers、body,带上cookie就好了
因为是查询一组数据,所有先把查询对象变量化(可以写到excel里去读,这里就不介绍了,提供这个思路)
构造头信息,直接复制
执行请求步骤,这一步是核心内容
完整代码
import grequests import requests cookies = { '__guid': 'x', 'UM_distinctid': '1713e1c320b108-x-4e4c0f20-15f900-1713e1c320c299', 'token': 'Bearer xxx.xxx.K6Z2EvPnaKLZSJW2TGBJiLvVro8wnxpFbrMhcfkHmVm8', 'online.xxx.com_mid3503': 'xxx', 'wx_Guestcookie19009': '%7b%22StoreId%22%xx%2c%xx%22%3a0%2c%x%22%3a%22%22%2c%22QueitErr%22%3a%22%22%7d', 'wx_Storecookie19009': 'xx', 'online.xxx.com': 'xx', 'production.xxx.com': 'xxxx', 'production.xxx.com_mid47243': 'xxxxx', 'wx_Guestcookie2948': '%7b%22StoreId%22%3a2948%2c%xxx%22%3a0%2c%22WxOpenId%22%3a%22%22%2c%22QueitErr%22%3a%22%22%7d', 'vshop_cookie2948': 'xxxx', 'sensorsdata2015jssdkcross': '%7B%22distinct_id%22%3A%xxx-076acb82c3e26f-4e4c0f20-1440000-1713e1c047632b%22%2C%22%24device_id%22%3A%221713e1c0475449-076acb82c3e26f-4e4c0f20-1440000-1713e1c047632b%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_referrer%22%3A%22%22%2C%22%24latest_referrer_host%22%3A%22%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%7D%2C%22ip_address%22%3A%22175.0.128.172%22%7D', 'monitor_count': '15', } headers = { 'Connection': 'keep-alive', 'Pragma': 'no-cache', 'Cache-Control': 'no-cache', 'Accept': 'application/json, text/plain, */*', 'Origin': 'https://my.xxx.com', 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36', 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', 'Sec-Fetch-Site': 'same-origin', 'Sec-Fetch-Mode': 'cors', 'Referer': 'https://my.xxxx.com/Order/Delivered', 'Accept-Encoding': 'gzip, deflate, br', 'Accept-Language': 'zh-CN,zh;q=0.9', } data1 = {'ids': '1086131'} data2 = {'ids': '1086132'} data3 = {'ids': '1086133'} data4 = {'ids': '1086134'} data5 = {'ids': '1086135'} data6 = {'ids': '1086136'} data7 = {'ids': '1086137'} data8 = {'ids': '1086138'} data9 = {'ids': '1086139'} data10 = {'ids': '1086140'} req_list = [ # 请求列表 grequests.post('https://my.xx.com/Order/ConfirmDelivery', headers=headers, cookies=cookies, data=data1), grequests.post('https://my.xxx.com/Order/ConfirmDelivery', headers=headers, cookies=cookies, data=data2), grequests.post('https://my.xxx.com/Order/ConfirmDelivery', headers=headers, cookies=cookies, data=data3), grequests.post('https://my.xxx.com/Order/ConfirmDelivery', headers=headers, cookies=cookies, data=data4), grequests.post('https://my.xxx.com/Order/ConfirmDelivery', headers=headers, cookies=cookies, data=data4), grequests.post('https://my.xx.com/Order/ConfirmDelivery', headers=headers, cookies=cookies, data=data5), grequests.post('https://my.xxx.com/Order/ConfirmDelivery', headers=headers, cookies=cookies, data=data6), grequests.post('https://my.xxx.com/Order/ConfirmDelivery', headers=headers, cookies=cookies, data=data7), grequests.post('https://my.xxx.com/Order/ConfirmDelivery', headers=headers, cookies=cookies, data=data8), grequests.post('https://my.xx.com/Order/ConfirmDelivery', headers=headers, cookies=cookies, data=data10), ] res_list = grequests.map(req_list) # 并行发送,等最后一个运行完后返回 print(res_list[0].text) # 打印第一个请求的响应文本