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

网页一直报错,萌新求解答哇

  •  
  •   gayu · 2016-10-26 10:55:06 +08:00 · 1858 次点击
    这是一个创建于 2741 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我在页面上输入的是: import requests

    head={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36','Accept-Encoding':'gzip, deflate, sdch'} url='https://diviner.jd.com/diviner?lid=1&p=103003&sku=676676' html=requests.get(url,headers=head) print html.content

    但是输出的却一直是

    <html> <head><title>500 Internal Server Error</title></head> <body bgcolor="white"> <center>

    500 Internal Server Error

    </center>
    <center>nginx</center> </body> </html>

    不知道是哪里出现问题了,这个网页我确实能打开的哇,求解答,谢谢

    4 条回复    2016-10-27 14:01:50 +08:00
    misaka19000
        1
    misaka19000  
       2016-10-26 16:13:29 +08:00
    很明显请求头部的信息不够,导致了服务器非正常响应
    gayu
        2
    gayu  
    OP
       2016-10-26 19:05:35 +08:00
    @misaka19000 需要全部加进去么?
    misaka19000
        3
    misaka19000  
       2016-10-26 20:17:35 +08:00
    @gayu 应该要,看服务器怎么解析了
    bocy
        4
    bocy  
       2016-10-27 14:01:50 +08:00
    要传一个 cookie ,可以加在头上,我这里试了一下这个代码可以返回数据

    ```
    import requests
    head={'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36','Accept-Encoding':'gzip, deflate, sdch',
    'Cookie': '__jdv=122270672|direct|-|none|-|1476333014350; mt_xid=V2_52007VwMRUFxQUl8WTR5sDWIGFlZaUFdGGE0eWhliVxIAQQtTUhdVGVpRNQdAAA1dVVlIeRpdBWAfElNBWFBLH0oSXgRsBhZiX2hSah9LH1wEbgIXYl1eVl4%3D; ipLocation=%u5E7F%u4E1C; ipLoc-djd=19-1607-40152-0; user-key=f20ed039-cf5c-4e40-91a5-a6035a957ac4; cn=0; atw=6881.2490744.12|655.1861125.7; __jda=122270672.1230914856.1474448839.1476333014.1477539207.2; __jdb=122270672.1.1230914856|2.1477539207; __jdc=122270672; __jdu=1230914856'
    }
    url='https://diviner.jd.com/diviner?lid=1&p=103003&sku=676676'
    html=requests.get(url,headers=head)
    print html.content

    ```
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3561 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 04:38 · PVG 12:38 · LAX 21:38 · JFK 00:38
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.