python无法调用urlopen_python urlopen 不了一个url 而用curl 是可以的

在尝试使用Python的urllib2库打开一个URL时遇到HTTP Error 400: BAD_REQUEST错误,而使用curl命令则可以成功。问题可能与请求的格式或库的处理方式有关。分析并解决Python中urllib2调用URL的错误。
摘要由CSDN通过智能技术生成

url=“http://redirect.viglink.com?key=4f41d321ffc89f8abfd39904671d7ab7&u=http%3A%2F%2Fwww.newegg.com%2FProduct%2FProduct.aspx%3Fnm_mc%3DAFC-dealmoon%26cm_mmc%3DAFC-dealmoon-_-NA-_-NA-_-NA%26Item%3DN82E16820147192”

import urllib2

next_url=urllib2.urlopen(url).geturl()

print next_url

Traceback (most recent call last):

File "", line 1, in

File "/usr/lib64/python2.6/urllib2.py", line 126, in urlopen

return _opener.open(url, data, timeout)

File "/usr/lib64/python2.6/urllib2.py", line 397, in open

response = meth(req, response)

File "/usr/lib64/python2.6/urllib2.py", line 510, in http_response

'http', request, response, code, msg, hdrs)

File "/usr/lib64/python2.6/urllib2.py", line 435, in error

return self._call_chain(*args)

File "/usr/lib64/python2.6/urllib2.py", line 369, in _call_chain

result = func(*args)

File "/usr/lib64/python2.6/urllib2.py", line 518, in http_error_default

raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)

urllib2.HTTPError: HTTP Error 400: BAD_REQUEST

而curl 是可以的[root@123 test]# curl -I 'http://redirect.viglink.com?key=4f41d321ffc89f8abfd39904671d7ab7&u=http%3A%2F%2Fwww.newegg.com%2FProduct%2FProduct.aspx%3Fnm_mc%3DAFC-dealmoon%26cm_mmc%3DAFC-dealmoon-_-NA-_-NA-_-NA%26Item%3DN82E16820147192'

HTTP/1.1 307 Temporary Redirect

Cache-Control: no-cache

Cache-Control: no-store

Content-length: 0

Date: Tue, 26 Feb 2013 09:03:13 GMT

Expires: Thu, 01 Jan 1970 00:00:00 GMT

Location: http://viglink.pgpartner.com/rd.php?r=280&m=1069763726&q=n&rdgt=1361801639&it=1361974439&et=1362406439&priceret=149.99&pg=~~3&k=3e4617236e1334fcb8741d8ab57a3b72&source=feed&url=http%3A%2F%2Fwww%2Enewegg%2Ecom%2FProduct%2FProduct%2Easpx%3FItem%3DN82E16820147192%26nm%5Fmc%3DOTC%2Dpr1c3grabb3r%26cm%5Fmmc%3DOTC%2Dpr1c3grabb3r%2D%5F%2DSolid%2BState%2BDisk%2D%5F%2DSAMSUNG%2D%5F%2D20147192&st=feed&mt=~~~~~~~viglink_106942~n~~~

Pragma: no-cache

Server: Apache-Coyote/1.1

Connection: keep-alive

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值