python用urlopen返回 HTTP Error 400,用curl正常,urlopencurl,url=“h
python用urlopen返回 HTTP Error 400,用curl正常,urlopencurl,url=“h
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 urllib2next_url=urllib2.urlopen(url).geturl()print next_urlTraceback (most recent call last): File "<stdin>", line 1, in <module> 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 RedirectCache-Control: no-cacheCache-Control: no-storeContent-length: 0Date: Tue, 26 Feb 2013 09:03:13 GMTExpires: Thu, 01 Jan 1970 00:00:00 GMTLocation: 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-cacheServer: Apache-Coyote/1.1Connection: keep-alive
话说你是要得到跳转之后的地址么?好像就是原网址后面带的这个网址吧?
import urllibnext_url = urllib.unquote(url.split('&u=')[1])
虽然跟用geturl()拿到的网址有些区别,不过打开看好像打开都是一个地方的说?刚学Python献丑了瓦~
p.s.我跟@greatghoul 一样,你给的代码可以正常执行的说~
不知道你的平台是什么
我在 Ubuntu 12.04 32bit + Python 2.7.3 下正常打开
>>> 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%3DN82E16820147192KeyboardInterrupt>>> next_url=urllib2.urlopen(url).geturl()>>> >>> print next_urlhttp://www.newegg.com/Product/Product.aspx?Item=N82E16820147192&nm_mc=OTC-pr1c3grabb3r&cm_mmc=OTC-pr1c3grabb3r-_-Solid+State+Disk-_-SAMSUNG-_-20147192>>>
编橙之家文章,
相关内容
- python运行爬虫程序时间如何控制?,python运行爬虫程序
- Python禁止生成pyc文件解决方法是什么,pythonpyc,使用py
- Django搭建网站二级目录内网页没有CSS样式求解决方式,
- Python部署方式最好用的是哪几种,python部署方式几种
- Python js计算0.7+0.1结果是什么,python0.7,今天在写 JS 的时
- Python BeautifulSoup解析html时部分源码丢失怎么办?,,在处
- python2字节码编码问题求助,python2字节码,# -*- coding
- python pip安装路径是放到/usr/lib or /usr/lib64,piplib64,/usr
- 批量缩进python项目重新格式化什么方法好,缩进python项
- Python与PHP哪个学习周期更短呢?,pythonphp周期,0 基础
评论关闭