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

求大神解读,帮助小白噻

  •  
  •   Number13 · Dec 7, 2017 · 3056 views
    This topic created in 3100 days ago, the information mentioned may be changed or developed.
    improt urllib3

    url = 抓取地址
    http = urllib3.PoolManager()
    result = http.request('GET', url)
    r = result.data

    这读取回来的数据里汉字全都是‘\xe5\xad\xa6 ’类似这样的数据,一旦用.decode('utf8')解码的话,就会少一部分数据,而少的那一部分数据正是我要的,恐怖不,这该咋整,萌新求助!求带飞
    11 replies    2017-12-08 09:51:52 +08:00
    mmnsghgn
        1
    mmnsghgn  
       Dec 7, 2017 via iPhone   ❤️ 1
    Carseason
        2
    Carseason  
       Dec 7, 2017 via iPhone
    你需要框架来帮助奴解析一些 js,还有记得使用 head 头
    ioven
        3
    ioven  
       Dec 7, 2017
    import requests

    r = requests.get(url)
    print(r.text)
    lhx2008
        4
    lhx2008  
       Dec 7, 2017 via Android
    小白用 request
    likuku
        5
    likuku  
       Dec 7, 2017
    小白用 request +10086
    likuku
        6
    likuku  
       Dec 7, 2017
    额,是 requests
    Number13
        7
    Number13  
    OP
       Dec 7, 2017
    然鹅,用 requests 介个还是丢数据,就是我要的数据用 php file_get_contents()就能拿到,但 python 拿回字节对象时,解码后就没了……这我就……
    Number13
        8
    Number13  
    OP
       Dec 7, 2017
    再次尝试 接:
    r = result.data.decode('utf-8')

    这里回显的数据仍然是没有想要的部分,但是字节对象中却是有
    大胆的先用正则取一下解码后的数据,居然取到了(ΩДΩ) 很是神奇啊,同样的代码,用 python 的 shell 跑一下,居然可以回显出想要的数据,很是神奇
    sosilver
        9
    sosilver  
       Dec 7, 2017 via Android
    返回的网页用的什么编码方式?
    Number13
        10
    Number13  
    OP
       Dec 7, 2017
    @sosilver 目标网址是 utf-8 的编码
    Number13
        11
    Number13  
    OP
       Dec 8, 2017
    炸了,找到症结所在,pycharm 对一些转码可能有缺陷,在控制台这样输出 print('-------测试数据---------\r\t') 这文字就没了…………好了就此结贴
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3262 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 42ms · UTC 13:14 · PVG 21:14 · LAX 06:14 · JFK 09:14
    ♥ Do have faith in what you're doing.