jjzjj

python - urllib.urlopen 有效,但 urllib2.urlopen 无效

我有一个正在测试的简单网站。它在本地主机上运行,​​我可以在我的网络浏览器中访问它。索引页就是简单的“运行”二字。urllib.urlopen将成功读取页面,但urllib2.urlopen不会。这是演示问题的脚本(这是实际脚本,而不是不同测试脚本的简化):importurllib,urllib2printurllib.urlopen("http://127.0.0.1").read()#prints"running"printurllib2.urlopen("http://127.0.0.1").read()#throwsanexception这是堆栈跟踪:Traceback(mos

python - urllib2.urlopen() 缓存东西吗?

他们没有在python文档中提到这一点。最近我正在测试一个网站,只是使用urllib2.urlopen()刷新网站以提取某些内容,有时我注意到当我更新网站时urllib2.urlopen()似乎没有获得新添加的内容。所以我想知道它确实在某处缓存了东西,对吧? 最佳答案 SoIwonderitdoescachestuffsomewhere,right?事实并非如此。如果您没有看到新数据,可能有多种原因。出于性能原因,大多数较大的Web服务都使用服务器端缓存,例如使用Varnish和Squid等缓存代理或应用程序级缓存。如果问题是由服务

python - urrlib2.urlopen : "Name or service not known" persists when starting script without internet connection

我在下面有这个简单的最小“工作”示例,它每两秒打开一次与谷歌的连接。当我在有可用的互联网连接时运行此脚本时,我会收到成功消息,然后当我断开连接时,我会收到失败消息,当我再次重新连接时,我会再次收到成功消息。到目前为止,还不错。但是,当我在互联网断开连接时启动脚本时,我收到失败消息,而当我稍后连接时,我再也没有收到成功消息。我不断收到错误消息:urlopenerror[Errno-2]Nameorservicenotknown这是怎么回事?importurllib2,timewhileTrue:try:print('Trying')response=urllib2.urlopen('ht

python - 如何从 urllib.urlopen() 返回的 “file-like object” 创建一个 GzipFile 实例?

我正在使用Python研究StackOverflowAPI。我正在尝试解码API提供的压缩响应。importurllib,gzipurl=urllib.urlopen('http://api.stackoverflow.com/1.0/badges/name')gzip.GzipFile(fileobj=url).read()根据theurllib2documentation,urlopen“返回一个类似文件的对象”。但是,当我在使用它创建的GzipFile对象上运行read()时,出现此错误:AttributeError:addinfourlinstancehasnoattribut

Python:URLError:<urlopen 错误 [Errno 10060]

操作系统:Windows7;使用PythonGUIShell的Python2.7.3我正在尝试通过Python读取一个网站,一些作者使用了urllib和urllib2库。为了将站点存储在变量中,我看到了一种类似的方法:importurllibimporturllib2g="http://www.google.com/"read=urllib2.urlopen(g)最后一行在120多秒后生成错误:>Traceback(mostrecentcalllast):File"",line1,in>>r=urllib2.urlopen(o)File"C:\Python27\lib\urllib2.

python - 获取 URLError : <urlopen error [Errno 111] Connection refused> in selenium webdriver using python in phantomjs

我在Python中使用SeleniumWebdriver。在headless浏览器(phantomjs)中运行脚本时出现URLError:谁能帮忙解决这个问题? 最佳答案 用谷歌搜索后,它似乎不可预测地发生了,但在我用Selenium/Phantom提出了一些请求之后。如果我使用driver=webdriver.PhantomJS()创建一个新的webdriver,那么我就可以继续发出请求。我建议其他人使用相同的解决方法。 关于python-获取URLError:inseleniumwe

python - urllib2.urlopen() 与 urllib.urlopen() - urllib2 在 urllib 工作时抛出 404!为什么?

importurllibprinturllib.urlopen('http://www.reefgeek.com/equipment/Controllers_&_Monitors/Neptune_Systems_AquaController/Apex_Controller_&_Accessories/').read()上述脚本工作并返回预期结果,同时:importurllib2printurllib2.urlopen('http://www.reefgeek.com/equipment/Controllers_&_Monitors/Neptune_Systems_AquaControl

python - 为什么 Python 的 urllib2.urlopen() 会为成功的状态码引发 HTTPError?

根据theurllib2documentation,Becausethedefaulthandlershandleredirects(codesinthe300range),andcodesinthe100-299rangeindicatesuccess,youwillusuallyonlyseeerrorcodesinthe400-599range.还有下面的代码request=urllib2.Request(url,data,headers)response=urllib2.urlopen(request)使用代码201(已创建)引发HTTPError:ERROR2011-08-1

python - 使用 urllib2.urlopen 时如何获取最终重定向 URL?

我正在使用urllib2.urlopen方法打开URL并获取网页的标记。其中一些网站使用301/302重定向重定向我。我想知道我被重定向到的最终URL。我怎样才能得到这个? 最佳答案 调用返回文件对象的.geturl()方法。根据urllib2docs:geturl()—returntheURLoftheresourceretrieved,commonlyusedtodetermineifaredirectwasfollowed例子:importurllib2response=urllib2.urlopen('http://tiny

Python 2.7.10 错误 "from urllib.request import urlopen"没有名为请求的模块

我从github打开了python代码。我以为它是python2.x并在我尝试运行它时出现上述错误。从阅读中我看到Python3已经贬低了urllib本身,并将其替换为包括urllib.request在内的许多库。看起来代码是用python3编写的(如果知道的人确认一下,将不胜感激。)在这一点上,我不想迁移到Python3——我还没有研究过它会对我现有的代码做什么。认为Python2应该有一个urllib模块,我搜索了谷歌(使用“python2urllib下载”)并没有找到。(由于urllib包含下载功能,它可能已隐藏在许多答案中。)我查看了我的Python27/lib目录,但在那里没