推荐学习书目
› 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
hukangha
V2EX  ›  Python

抓网页,数据无法解码,看着不像编码给错

  •  
  •   hukangha · Dec 17, 2017 · 4092 views
    This topic created in 3247 days ago, the information mentioned may be changed or developed.

    初学... 代码是这样:

    from html.parser import HTMLParser
    import urllib.request
    import chardet
    
    pars = HTMLParser()
    home_url = "https://wallstreetcn.com/"
    response = urllib.request.urlopen(home_url)
    content = response.read()
    encoding = chardet.detect(content)
    pars.feed(content.decode(encoding["encoding"],errors="ignore"))
    

    chrome 看网页 metadata 里面 charset 用的 utf-8,我这里无论直接用'utf-8' 还是检测编码,均无法正确解码,有点 response 根本就没给出正确数据的感觉。请教一下

    7 replies  •  2017-12-27 15:59:04 +08:00
    n329291362
    n329291362
    Dec 17, 2017❤️1
    1
    1f8b 开头。。。。gzip 压缩啊 最简单的
    import gzip
    gzip.decompress(content)
    swordspoet
    swordspoet
    Dec 18, 2017 via iPhone
    2
    换一个 HTML 解析器,html.parser 的容错率不高,试试看 lxml
    swordspoet
    swordspoet
    Dec 18, 2017 via iPhone
    3
    from bs4 import BeautifulSoup

    standard_html = BeautifulSoup(content, 'lxml')

    试试看这个~
    free9fw
    free9fw
    Dec 18, 2017
    4
    Accept-Encoding:gzip, deflate, br
    hukangha
    hukangha
    OP
    Dec 18, 2017
    5
    @n329291362
    果然... 可是如果是其他的压缩什么的怎么办... 只能这样靠丰富的经验么...
    n329291362
    n329291362
    Dec 19, 2017
    6
    @hukangha
    也可以控制头 Accept-Encoding 让服务端返回没压缩过的数据
    网页最多也就是 gzip 压缩 在复杂也复杂不到哪
    F1024
    F1024
    Dec 27, 2017
    7
    import os
    import requests

    html = requests.get('https://wallstreetcn.com').content.decode('utf-8')
    print(html)

    os.system("pause")

    是这个吗
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   695 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 20:48 · PVG 04:48 · LAX 12:48 · JFK 15:48
    ♥ Do have faith in what you're doing.