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)  # 打印第一個請求的響應文本

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM