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
zzzkkk
V2EX  ›  Python

Python 爬虫返回加密数据\x

  •  
  •   zzzkkk · 2022-09-12 14:27:34 +08:00 · 1686 次点击
    这是一个创建于 585 天前的主题,其中的信息可能已经有所发展或是发生改变。
    很简单的代码
    if __name__ == '__main__':
    headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:104.0) Gecko/20100101 Firefox/104.0",
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
    "Accept-Language": "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2",
    "Accept-Encoding": "gzip, deflate, br",
    "Connection": "keep-alive",
    "Upgrade-Insecure-Requests": "1",
    "Sec-Fetch-Dest": "iframe",
    "Sec-Fetch-Mode": "navigate",
    "Sec-Fetch-Site": "same-site"
    }
    import urllib.request
    req = urllib.request.Request("https://www.ebay.com", headers = headers)
    resp = urllib.request.urlopen(req)
    print(resp.read())

    返回的都是 xfb\xb9\xbd\x8e\x92Q\xda\xbf\x1b\x9b\xe2i\xc7\xed>n\x
    4 条回复    2022-09-12 16:00:52 +08:00
    Alexonx
        1
    Alexonx  
       2022-09-12 14:45:57 +08:00
    这不是加密 这是 GZip 压缩了 把 Accept-Encoding 里面的 gzip 去了试试
    learningman
        2
    learningman  
       2022-09-12 15:14:15 +08:00   ❤️ 2
    新手换 requests
    filwaline
        3
    filwaline  
       2022-09-12 15:15:00 +08:00
    @Alexonx
    正解,或者是加行代码来解压

    ```
    import gzip
    content = gzip.decompress(resp.read())
    ```
    HankLu
        4
    HankLu  
       2022-09-12 16:00:52 +08:00
    requests 不香吗?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5473 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 06:34 · PVG 14:34 · LAX 23:34 · JFK 02:34
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.