标签: urlopen

如何处理"getaddrinfo失败"?

你好,我有问题.我使用mechanize,python 2.7连接一些网站(代码现在不重要)我有网站列表,我现在连接到它们.当它发生时我的列表中的网站不存在我得到错误:

urllib2.URLError:[Errno 11004] getaddrinfo失败

我尝试通过这样做来处理它:

             except mechanize.URLError, e:
                    result = str(e.reason)
Run Code Online (Sandbox Code Playgroud)

要么

             except urllib2.URLError, e:
                    result = str(e.reason)
Run Code Online (Sandbox Code Playgroud)

甚至

             except Exception, e:
                    result = str(e)
Run Code Online (Sandbox Code Playgroud)

但它只是不想工作.

怎么解决这个?当发生此错误时,我只想打印"连接失败"之类的内容并移动到列表中的下一个地址.如何捕获此错误except

python urllib urllib2 urlopen

3
推荐指数
1
解决办法
1万
查看次数

Mercurial和nginx.错误10054

当我将文件推送到服务器时,60秒后我得到"urlopen错误[Errno 10054]".我认为这是因为我推送大文件.我使用nginx web-server和fast-cgi进程.什么nginx参数负责60秒超时?或许我应该看到另一种方式?

mercurial timeout nginx urlopen

3
推荐指数
1
解决办法
2332
查看次数

在Python中使用urllib.request.urlopen时出错

这段代码出了什么问题?

>>> from urllib.request import urlopen
>>> for line in urlopen("http://google.com/"):
       print(line.decode("utf-8"))


<!doctype html><html><head><meta http-equiv="content-type" content="text/html; charset=windows-1251"><title>Google</title><script>window.google={kEI:"XMECT7XyDcGn0AWFk7ywAQ",getEI:function(a){var b;while(a&&!(a.getAttribute&&(b=a.getAttribute("eid"))))a=a.parentNode;return b||google.kEI},https:function(){return window.location.protocol=="https:"},kEXPI:"33492,35300",kCSI:{e:"33492,35300",ei:"XMECT7XyDcGn0AWFk7ywAQ"},authuser:0,

ml:function(){},kHL:"uk",time:function(){return(new Date).getTime()},log:function(a,b,c,e){var d=new Image,g=google,h=g.lc,f=g.li,j="";d.onerror=(d.onload=(d.onabort=function(){delete h[f]}));h[f]=d;if(!c&&b.search("&ei=")==-1)j="&ei="+google.getEI(e);var i=c||"/gen_204?atyp=i&ct="+a+"&cad="+b+j+"&zx="+google.time(),k=/^http:/i;if(k.test(i)&&google.https()){google.ml(new Error("GLMM"),false,{src:i});

delete h[f];return}d.src=i;g.li=f+1},lc:[],li:0,Toolbelt:{},y:{},x:function(a,b){google.y[a.id]=

[a,b];return false}};

window.google.sn="webhp";window.google.timers={};window.google.startTick=function(a,b){window.google.timers[a]={t:{start:(new Date).getTime()},bfr:!(!b)}};window.google.tick=function(a,b,c){if(!window.google.timers[a])google.startTick(a);window.google.timers[a].t[b]=c||(new Date).getTime()};google.startTick("load",true);try{}catch(u){}

var _gjwl=location;function _gjuc(){var e=_gjwl.href.indexOf("#");if(e>=0){var a=_gjwl.href.substring(e);if(a.indexOf("&q=")>0||a.indexOf("#q=")>=0){a=a.substring(1);if(a.indexOf("#")==-1){for(var c=0;c<a.length;){var d=c;if(a.charAt(d)=="&")++d;var b=a.indexOf("&",d);if(b==-1)b=a.length;var f=a.substring(d,b);if(f.indexOf("fp=")==0){a=a.substring(0,c)+a.substring(b,a.length);b=c}else if(f=="cad=h")return 0;c=b}_gjwl.href="/search?"+a+"&cad=h";return 1}}}return 0}function _gjp(){!(window._gjwl.hash&&

window._gjuc())&&setTimeout(_gjp,500)};

Traceback (most recent call last):
  File "<pyshell#109>", line 2, in <module>
    print(line.decode("utf-8"))
UnicodeDecodeError: 'utf8' codec can't decode byte 0xc2 in position 2364: invalid continuation byte
Run Code Online (Sandbox Code Playgroud)

python urllib urlopen

3
推荐指数
1
解决办法
1535
查看次数

如何使用Python urlopen设置cookie?

我试图使用Python urlopen获取一个html站点.
我收到此错误:

HTTPError:HTTP错误302:HTTP服务器返回了重定向错误,导致无限循环

代码:

from urllib2 import Request
request = Request(url)
response = urlopen(request)
Run Code Online (Sandbox Code Playgroud)

我知道服务器重定向到另一个URL并且它正在寻找cookie.
如何设置它正在寻找的cookie,以便我可以阅读html?

python cookies redirect http-error urlopen

3
推荐指数
1
解决办法
7102
查看次数

在 Python 中使用 urlopen() 防止“隐藏”重定向

我正在使用BeautifulSoup进行网页抓取,并且在使用urlopen时遇到特定类型网站的问题。网站上的每个商品都有自己独特的页面,并且商品有不同的规格(例如:500 mL、1L、2L...)。

当我使用互联网浏览器打开产品的 URL ( www.example.com/product1 ) 时,我会看到 500 毫升规格的图片、相关信息(价格、数量、口味等)以及列表此特定项目可用的所有其他格式。如果点击另一种格式(例如: 1L),有关该项目的图片和信息将会更改,但浏览器顶部的 URL 将保持不变(www.example.com/product1)。但是,通过检查页面的 HTML 代码,我知道所有格式都有自己唯一的 URL(500 mL:www.example.com/product1/123;1L:www.example.com/product1/456,...)。当我在互联网浏览器中使用 1L 格式的唯一 URL 时,我会自动重定向到页面www.example.com/product1,但页面上显示的图片和信息对应于 1L 格式。HTML 代码还包含我需要的有关 1L 格式的信息。

当我使用urlopen打开这些唯一的 URL时,出现了问题。

from bs4 import BeautifulSoup 
from urllib import urlopen
webpage = urlopen('www.example.com/product1/456')
soup=BeautifulSoup(webpage)
print soup    
Run Code Online (Sandbox Code Playgroud)

汤中包含的信息与使用我的互联网浏览器显示的唯一 URL 的信息不对应: www.example.com/product1/456它为我提供了有关www.example.com/product1上默认显示的项目格式的信息,该格式始终为 500 mL 格式。

有什么方法可以阻止这种重定向,从而允许我使用 BeautifulSoup 捕获唯一 URL 的 HTML 代码中包含的信息?

python urllib beautifulsoup urlopen

3
推荐指数
1
解决办法
1872
查看次数

Python:urlopen - 如果发生任何错误,则跳过条目

我想知道 urlopen 是否有某种“全部捕获”代码,如果访问网站时发生任何错误,它会跳过我的 for 循环中的整个条目。

python urllib2 urlopen

3
推荐指数
1
解决办法
3096
查看次数

urllib.request.urlopen返回字节,但我无法解码它

我尝试使用解析网页urllib.requesturlopen()方法,如:

from urllib.request import Request, urlopen
req = Request(url)
html = urlopen(req).read()
Run Code Online (Sandbox Code Playgroud)

但是,最后一行以字节为单位返回结果.

所以我尝试解码它,如:

html = urlopen(req).read().decode("utf-8")
Run Code Online (Sandbox Code Playgroud)

但是,发生了错误:

UnicodeDecodeError:'utf-8'编解码器无法解码位置1中的字节0x8b:无效的起始字节.

通过一些研究,我找到了一个相关的答案,解析charset决定解码.但是,该页面不会返回字符集,当我尝试在Chrome Web Inspector上进行检查时,其标题中会写入以下行:

<meta charset="utf-8">
Run Code Online (Sandbox Code Playgroud)

那么为什么我不能解码呢utf-8?我如何成功解析网页?

网站URL是http://www.vogue.com/fashion-shows/fall-2016-menswear/fendi/slideshow/collection#2,我想将图像保存到我的磁盘.

请注意,我使用的是Python 3.5.1.我还注意到我上面写的所有工作在我的其他抓取程序中运行良好.

python decode urllib urlopen python-3.x

3
推荐指数
1
解决办法
3399
查看次数

为什么urllib2.urlopen无法打开像"http:// localhost/new-post#comment-29"这样的网页?

我很好奇,为什么我在运行这行时遇到404错误:

urllib2.urlopen("http://localhost/new-post#comment-29")
Run Code Online (Sandbox Code Playgroud)

虽然一切正常,但在任何浏览器中浏览http:// localhost/new-post#comment-29 ...

urlopen方法不解析带有"#"的网址?

有人知道吗?

python urllib2 urlopen fragment-identifier

2
推荐指数
1
解决办法
1121
查看次数

使用urllib2.urlopen时,如何访问包含重定向的原始响应标头

我正在尝试解析使用urllib2.urlopen后返回的HTTP响应的位置标头,但我收到的唯一响应标头来自目标重定向 - 而不是包含位置标头的原始响应.

我已经关注了Stack Overflow的其他问题,建议将urllib2.HTTPRedirectHandler子类化,但是我仍然无法理解如何访问urlopen最终跟随的原始响应.

这是一个问题的例子:

import urllib2

req = urllib2.urlopen("http://wp.me")

print req.info()
Run Code Online (Sandbox Code Playgroud)

print的输出包含重定向请求的目标的响应头.我想看看原版.

任何帮助,将不胜感激.

python location header urllib2 urlopen

2
推荐指数
1
解决办法
1746
查看次数

Python 3.6 urllib 为什么行以 b 开头

我正在使用 python 3,并且不明白为什么输出在每个行的开头都有 b 。我不认为 python 2 是这种情况。为什么会这样以及如何删除它?谢谢

import urllib
# fhand = urllib.urlopen('http://www.py4inf.com/code/romeo.txt') in Python 2
fhand = urllib.request.urlopen('http://www.py4inf.com/code/romeo.txt')
for line in fhand:
    print(line.strip())
Run Code Online (Sandbox Code Playgroud)

输出看起来像这样

b'But soft what light through yonder window breaks'
b'It is the east and Juliet is the sun'
b'Arise fair sun and kill the envious moon'
b'Who is already sick and pale with grief'
Run Code Online (Sandbox Code Playgroud)

urllib request urlopen python-3.x

2
推荐指数
1
解决办法
3264
查看次数