有時(shí)候需要處理很多請(qǐng)求,顯然,一個(gè)一個(gè)去處理是要花費(fèi)很多時(shí)間的
我們就需要用到并發(fā)的方式溪食,python并發(fā)請(qǐng)求的方法很多,從簡(jiǎn)單到復(fù)雜娜扇。
介紹一個(gè)超級(jí)簡(jiǎn)單错沃,使用grequests庫,實(shí)現(xiàn)并發(fā)請(qǐng)求
接口參數(shù)狠簡(jiǎn)單雀瓢,headers枢析、body,帶上cookie就好了
因?yàn)槭遣樵円唤M數(shù)據(jù)刃麸,所有先把查詢對(duì)象變量化(可以寫到excel里去讀醒叁,這里就不介紹了,提供這個(gè)思路)
構(gòu)造頭信息泊业,直接復(fù)制
執(zhí)行請(qǐng)求步驟把沼,這一步是核心內(nèi)容
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 = [ # 請(qǐng)求列表
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) # 并行發(fā)送,等最后一個(gè)運(yùn)行完后返回
print(res_list[0].text) # 打印第一個(gè)請(qǐng)求的響應(yīng)文本