grequest案例对比requests案例
grequets和requests案例各一个,使用上对比:
import grequests import requests headers = { "content-type": "application/json;charset=UTF-8", "authentication": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9" ".eyJpc3MiOiJwYXNzcG9ydHRlc3Quc2VuZ3VvLm1lIiwiZXhwIjoxNjA5MjI4MzczLCJwYXNzcG9ydF9pZCI6MzI4OTg4MCwic3ViIjoiYXV0aF90b2tlbiIsImF1ZCI6ImxpbmdzaG91IiwiaWF0IjoxNjA4NjIzNTczfQ.UiKxI2gtDy9vl43F_i4305KaGeADAyuTpJxSwtXqAfY", "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) " "Chrome/72.0.3626.121 Safari/ " } url = 'https://cashiertest.sg.me/api/finance/getonedayshopprofit' data = '{"date": "2020-12-22"}' def g_request(): res = [grequests.post(url=url, data=data, headers=headers)] rp = grequests.map(res) for i in rp: print(i.text) def requests_(): res = requests.post(url=url, data=data, headers=headers) print(res.text) if __name__ == '__main__': g_request() requests_() 控制台输出都一样: {"message":"请求成功","data":{"shop_profit":{"is_confirmed":false,"gross_profit":null,"gross_profit_rate":null},"shop_goods_profits":[]}} {"message":"请求成功","data":{"shop_profit":{"is_confirmed":false,"gross_profit":null,"gross_profit_rate":null},"shop_goods_profits":[]}}
grequest标准使用:
import grequests # gevent+request request_list = [ grequests.get('https://www.runoob.com/python3/python3-file-methods.html', timeout=0.1), grequests.get('https://www.zxgj.cn/g/tupiancaijian'), grequests.get('https://www.json.cn/') ] # 响应列表 response_list = grequests.map(request_list) # 取响应码(和request用于) response_content = response_list[0].status_code # 取json # ips = [ret.json() for ret in response_list if ret and ret.status_code == 200] print(response_content)

浙公网安备 33010602011771号