V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
fanhaipeng0403
V2EX  ›  Python

求助, curl 转 Python 代码

  •  
  •   fanhaipeng0403 · 2023-09-16 09:27:34 +08:00 · 2393 次点击
    这是一个创建于 463 天前的主题,其中的信息可能已经有所发展或是发生改变。
    ```
    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 请求,就一直超时,
    请求什么原因
    7 条回复    2023-09-17 12:02:01 +08:00
    fanhaipeng0403
        1
    fanhaipeng0403  
    OP
       2023-09-16 09:37:14 +08:00
    >>> 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
        2
    sunfkny  
       2023-09-16 09:58:07 +08:00   ❤️ 3
    服务器只支持 HTTP2.0, 建议直接用 httpx 代替 requests,用法基本兼容
    pip install httpx[http2]

    https://gist.github.com/sunfkny/ca17708a4702486e2199c672e844342a
    22F41628gA98q4Lx
        3
    22F41628gA98q4Lx  
       2023-09-16 11:31:55 +08:00
    https://curlconverter.com/

    试试这个网站
    fanhaipeng0403
        4
    fanhaipeng0403  
    OP
       2023-09-16 12:16:28 +08:00
    @sunfkny 牛逼,感谢,祝你天天开心
    sofarsofunny1
        5
    sofarsofunny1  
       2023-09-16 22:03:12 +08:00
    @fanhaipeng0403 你要买二手特斯拉吗
    deorth
        6
    deorth  
       2023-09-16 23:06:21 +08:00 via Android
    还有人故意不开 http1.1 兼容的,长见识了。马一龙就是牛逼
    Cola98
        7
    Cola98  
       2023-09-17 12:02:01 +08:00
    你可以试下 postman ,可以把 curl 转换成 python 还有一些其他语言
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2917 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 12:59 · PVG 20:59 · LAX 04:59 · JFK 07:59
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.