【问题标题】:Specific string in python requests.get gives ValueErrorpython requests.get 中的特定字符串给出 ValueError
【发布时间】:2015-05-18 18:13:46
【问题描述】:

正在尝试获取此特定页面...

request = requests.get('http://market.yandex.ru/catalog/90555/list')

...给我一个奇怪的错误:

ValueError                                Traceback (most recent call last)
    C:\Python34\lib\site-packages\requests\packages\urllib3\response.py in read_chunked(self, amt)
    406                 try:
--> 407                     self.chunk_left = int(line, 16)
    408                 except ValueError:

ValueError: invalid literal for int() with base 16: ''

我发现应该归咎于字符串的某些部分。我正在试验它,结果更奇怪:

# No error
http://market.ru/catalog/90555/list
http://market.yandex.ru/catalo

# Error
http://market.yandex.ru/catalog

附:顺便说一句,今天出现了问题。就在最近,我在获取这个页面时没有任何问题(使用相同的方法)。

【问题讨论】:

  • 您是否 100% 确定您没有运行旧版本的库?您是否可能升级并忘记重新启动?回溯中的源代码表明应该捕获异常。
  • @MartijnPieters,我遇到了类似的问题,当我用浏览器打开页面时,它要求用下面的消息填写验证码
  • @PadraicCunningham:但不一样的回溯。我设法在requests 中很好地加载了页面,但如果他们实施的速率限制直接违反了分块传输编码的预期,那么urllib3 库将在那里引发IncompleteRead 异常。 ValueError 异常使得不被捕获毫无意义。
  • @MartijnPieters,你是怎么填写验证码的?
  • @SereznoKot:确实是这样,但实施得很糟糕。

标签: python python-3.x python-requests


【解决方案1】:

您受到速率限制,但服务器这样做的方式违反了 HTTP 规范。他们的响应标头承诺分块传输编码,然后不要发送这样的响应。

如果您在详细模式下查看带有curl 的 URL,您会得到以下输出:

$ curl -v https://market.yandex.ru/catalog/90555/list
* Hostname was NOT found in DNS cache
*   Trying 213.180.204.22...
* Connected to market.yandex.ru (213.180.204.22) port 443 (#0)
* TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256
* Server certificate: market.yandex.ru
* Server certificate: Certum Level IV CA
* Server certificate: Certum CA
> GET /catalog/90555/list HTTP/1.1
> User-Agent: curl/7.37.1
> Host: market.yandex.ru
> Accept: */*
> 
< HTTP/1.1 302 Found
* Server nginx is not blacklisted
< Server: nginx
< Date: Mon, 18 May 2015 18:53:15 GMT
< Content-Type: text/html; charset=UTF-8
< Transfer-Encoding: chunked
< Connection: keep-alive
< Keep-Alive: timeout=120
< X-Forwardtouser-Y: 1
< Set-Cookie: spravka=dD0xNDAwNDM5MTk1O2k9ODQuOTIuOTguMTcwO3U9MTQwMDQzOTE5NTUxNjUwOTExMjtoPWNkMzVlMzBlMjgxMTg4YWM0YjYyZDg3OTg4ZjUyNWFj; domain=.yandex.ru; path=/; expires=Wed, 17-Jun-2015 18:53:15 GMT
< Location: http://market.yandex.ru/showcaptcha?cc=1&retpath=http%3A//market.yandex.ru/catalog/90555/list%3F_bfd13d35fbf1551a835f050d3775fc4b&t=0/1431975195/029660aeb063916c78e30ebd9444fd4b&s=4dd645e7048b399008278208fa776ba9
< Set-Cookie: uid=CniLolVaNRthdR2JDtV0Ag==; path=/
< 
* transfer closed with outstanding read data remaining
* Closing connection 0
curl: (18) transfer closed with outstanding read data remaining

正在向您发送重定向,但响应中的Transfer-Encoding: chunked 标头意味着客户端必须加载不存在的块。

重定向导致验证码:

http://market.yandex.ru/showcaptcha?cc=1&retpath=http%3A//market.yandex.ru/catalog/90555/list%3F_bfd13d35fbf1551a835f050d3775fc4b&t=0/1431975195/029660aeb063916c78e30ebd9444fd4b&s=4dd645e7048b399008278208fa776ba9
#                       ^^^^^^^^^^^

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-04
    • 2014-04-19
    • 2016-04-07
    • 1970-01-01
    • 1970-01-01
    • 2020-11-08
    • 1970-01-01
    相关资源
    最近更新 更多