求助, curl 转 Python 代码

235 天前
 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 请求,就一直超时,
请求什么原因
2113 次点击
所在节点    Python
7 条回复
fanhaipeng0403
235 天前
>>> 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
235 天前
服务器只支持 HTTP2.0, 建议直接用 httpx 代替 requests,用法基本兼容
pip install httpx[http2]

https://gist.github.com/sunfkny/ca17708a4702486e2199c672e844342a
Tstxxy
235 天前
https://curlconverter.com/

试试这个网站
fanhaipeng0403
235 天前
@sunfkny 牛逼,感谢,祝你天天开心
sofarsofunny1
234 天前
@fanhaipeng0403 你要买二手特斯拉吗
deorth
234 天前
还有人故意不开 http1.1 兼容的,长见识了。马一龙就是牛逼
Cola98
234 天前
你可以试下 postman ,可以把 curl 转换成 python 还有一些其他语言

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/974328

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX