求助, curl 转 Python 代码

查看 58|回复 2
作者:fanhaipeng0403   
```
curl 'https://www.tesla.cn/inventory/api/v1/inventory-results?query=%7B%22query%22%3A%7B%22model%22%3A%22ms%22%2C%22condition%22%3A%22used%22%2C%22options%22%3A%7B%22FleetSalesRegions%22%3A%5B%22CN%22%5D%7D%2C%22arrangeby%22%3A%22Price%22%2C%22order%22%3A%22asc%22%2C%22market%22%3A%22CN%22%2C%22language%22%3A%22zh%22%2C%22super_region%22%3A%22north%20america%22%2C%22lng%22%3A%22%22%2C%22lat%22%3A%22%22%2C%22zip%22%3A%22%22%2C%22range%22%3A0%7D%2C%22offset%22%3A0%2C%22count%22%3A50%2C%22outsideOffset%22%3A0%2C%22outsideSearch%22%3Afalse%7D' \
-H 'sec-ch-ua: "Chromium";v="116", "Not)A;Brand";v="24", "Google Chrome";v="116"' \
-H 'Referer: https://www.tesla.cn/inventory/used/ms?Province=CN&FleetSalesRegions=CN' \
-H 'sec-ch-ua-mobile: ?0' \
-H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36' \
-H 'sec-ch-ua-platform: "macOS"' \
--compressed

```

```
import requests
headers = {
'sec-ch-ua': '"Chromium";v="116", "Not)A;Brand";v="24", "Google Chrome";v="116"',
'Referer': 'https://www.tesla.cn/inventory/used/ms?Province=CN&FleetSalesRegions=CN',
'sec-ch-ua-mobile': '?0',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36',
'sec-ch-ua-platform': '"macOS"',
}
params = {
'query': '{"query":{"model":"ms","condition":"used","options":{"FleetSalesRegions":["CN"]},"arrangeby":"Price","order":"asc","market":"CN","language":"zh","super_region":"north america","lng":"","lat":"","zip":"","range":0},"offset":0,"count":50,"outsideOffset":0,"outsideSearch":false}',
}
response = requests.get('https://www.tesla.cn/inventory/api/v1/inventory-results', params=params, headers=headers)
```
这个 curl 请求语句放在 shell 里能请求成功,但是转为 python 代码,或者导入 postman 请求,就一直超时,
请求什么原因

Chrome, curl, 请求

fanhaipeng0403
OP
  
>>> headers
{'sec-ch-ua': '"Chromium";v="116", "Not)A;Brand";v="24", "Google Chrome";v="116"', 'Referer': 'https://www.tesla.cn/inventory/used/ms?Province=CN&FleetSalesRegions=CN', 'sec-ch-ua-mobile': '?0', 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36', 'sec-ch-ua-platform': '"macOS"'}
>>> url='https://www.tesla.cn/inventory/api/v1/inventory-results?
query=%7B%22query%22%3A%7B%22model%22%3A%22ms%22%2C%22condition%22%3A%22used%22%2C%22options%22%3A%7B%22FleetSalesRegions%22%3A%5B%22CN%22%5D%7D%2C%22arrangeby%22%3A%22Price%22%2C%22order%22%3A%22asc%22%2C%22market%22%3A%22CN%22%2C%22language%22%3A%22zh%22%2C%22super_region%22%3A%22north%20america%22%2C%22lng%22%3A%22%22%2C%22lat%22%3A%22%22%2C%22zip%22%3A%22%22%2C%22range%22%3A0%7D%2C%22offset%22%3A0%2C%22count%22%3A50%2C%22outsideOffset%22%3A0%2C%22outsideSearch%22%3Afalse%7D'
>>> response = requests.get(url, headers=headers)
一直超时
sunfkny   
服务器只支持 HTTP2.0, 建议直接用 httpx 代替 requests,用法基本兼容
pip install httpx[http2]
https://gist.github.com/sunfkny/ca17708a4702486e2199c672e844342a
您需要登录后才可以回帖 登录 | 立即注册

返回顶部