Python urllib2返回空字符串

2024-10-01 07:28:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试检索以下URL:http://www.winkworth.co.uk/sale/property/flat-for-sale-in-masefield-court-london-n5/HIH140004。在

import urllib2
response = urllib2.urlopen('http://www.winkworth.co.uk/rent/property/terraced-house-to-rent-in-mill-road--/WOT140129')
response.read()

但是我得到了一个空字符串。当我通过浏览器或者使用cURL来尝试它时,效果很好。你知道怎么回事吗?在


Tags: inhttpurlforresponsewwwpropertysale
1条回答
网友
1楼 · 发布于 2024-10-01 07:28:10

我在使用requests库时得到了响应,但在使用urllib2时没有得到响应,所以我尝试了HTTP请求头。在

事实证明,服务器需要一个Accept头;urllib2不发送,requests和cURL send */*。在

同时发送一个带有urllib2的邮件:

url = 'http://www.winkworth.co.uk/sale/property/flat-for-sale-in-masefield-court-london-n5/HIH140004'
req = urllib2.Request(url, headers={'accept': '*/*'})
response = urllib2.urlopen(req)

演示:

^{2}$

服务器出现故障;RFC 2616状态:

If no Accept header field is present, then it is assumed that the client accepts all media types.

相关问题 更多 >