urllib2

pip install urllib2 #failed [duplicate]

匿名 (未验证) 提交于 2019-12-03 02:41:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: This question already has an answer here: Python 3.2 Unable to import urllib2 (ImportError: No module named urllib2) [duplicate] 3 answers I am trying to install urllib2 in cmd console to import it from python 3.5. I am getting this error: pip install urllib2 Failed building wheel for urllib2 Command "c:\anaconda\python.exe -u -c "import setuptools, tokenize; file ='C:\Users\NA401134\AppData\Local\Temp\pip-build-7meqhp18\urllib2\setup.py';f=getattr(tokenize, 'open', open)( file );code=f.read().replace('\r\n', '\n');f.close();exec(compile

Python urllib2 force IPv4

匿名 (未验证) 提交于 2019-12-03 02:30:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am running a script using python that uses urllib2 to grab data from a weather api and display it on screen. I have had the problem that when I query the server, I get a "no address associated with hostname" error. I can view the output of the api with a web browser, and I can download the file with wget, but I have to force IPv4 to get it to work. Is it possible to force IPv4 in urllib2 when using urllib2.urlopen? 回答1: Not directly, no. So, what can you do? One possibility is to explicitly resolve the hostname to IPv4 yourself, and then

Python handling socket.error: [Errno 104] Connection reset by peer

匿名 (未验证) 提交于 2019-12-03 02:08:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: When using Python 2.7 with urllib2 to retrieve data from an API, I get the error [Errno 104] Connection reset by peer . Whats causing the error, and how should the error be handled so that the script does not crash? ticker.py def urlopen(url): response = None request = urllib2.Request(url=url) try: response = urllib2.urlopen(request).read() except urllib2.HTTPError as err: print "HTTPError: {} ({})".format(url, err.code) except urllib2.URLError as err: print "URLError: {} ({})".format(url, err.reason) except httplib.BadStatusLine as err:

Python 3.2 Unable to import urllib2 (ImportError: No module named urllib2) [duplicate]

匿名 (未验证) 提交于 2019-12-03 02:05:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: This question already has an answer here: Import error: No module name urllib2 7 answers I am using Windows, and I get the error: ImportError : No module named urllib2 I think this is the solution for Linux. But how to set this in Windows? I am using Python 3.2 and I am not able see urllib2 there in the LiB folder. 回答1: In python 3 urllib2 was merged into urllib. See also another Stack Overflow question and the urllib PEP 3108 . To make Python 2 code work in Python 3: try : import urllib . request as urllib2 except ImportError :

HTTPError: HTTP Error 403: Forbidden

匿名 (未验证) 提交于 2019-12-03 01:59:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I making a python script for personal use but it's not working for wikipedia... This work: import urllib2, sys from bs4 import BeautifulSoup site = "http://youtube.com" page = urllib2.urlopen(site) soup = BeautifulSoup(page) print soup This not work: import urllib2, sys from bs4 import BeautifulSoup site= "http://en.wikipedia.org/wiki/StackOverflow" page = urllib2.urlopen(site) soup = BeautifulSoup(page) print soup This is the error: Traceback (most recent call last): File "C:\Python27\wiki.py", line 5, in page = urllib2.urlopen(site) File

retrieve links from web page using python and BeautifulSoup

匿名 (未验证) 提交于 2019-12-03 01:55:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: How can I retrieve the links of a webpage and copy the url address of the links using Python? 回答1: Here's a short snippet using the SoupStrainer class in BeautifulSoup: import httplib2 from BeautifulSoup import BeautifulSoup, SoupStrainer http = httplib2.Http() status, response = http.request('http://www.nytimes.com') for link in BeautifulSoup(response, parseOnlyThese=SoupStrainer('a')): if link.has_attr('href'): print link['href'] The BeautifulSoup documentation is actually quite good, and covers a number of typical scenarios: http://www

Python urllib2 + Beautifulsoup

匿名 (未验证) 提交于 2019-12-03 01:40:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: So I'm struggling to implement beautiful into my current python project, Okay so to keep this plain and simple I'll reduce the complexity of my current script. Script without BeautifulSoup - import urllib2 def check(self, name, proxy): urllib2.install_opener( urllib2.build_opener( urllib2.ProxyHandler({'http': 'http://%s' % proxy}), urllib2.HTTPHandler() ) ) req = urllib2.Request('http://example.com' ,"param=1") try: resp = urllib2.urlopen(req) except: self.insert() try: if 'example text' in resp.read() print 'success' now of course the

Python urllib2 Response header

匿名 (未验证) 提交于 2019-12-03 01:33:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm trying to extract the response header of a URL request. When I use firebug to analyze the response output of a URL request, it returns: Content-Type text/html However when I use the python code: urllib2.urlopen(URL).info() the resulting output returns: Content-Type: video/x-flv I am new to python, and to web programming in general; any helpful insight is much appreciated. Also, if more info is needed please let me know. Thanks in advance for reading this post 回答1: Try to request as Firefox does. You can see the request headers in Firebug

Python urllib2 with keep alive

匿名 (未验证) 提交于 2019-12-03 01:29:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: How can I make a "keep alive" HTTP request using Python's urllib2? 回答1: Use the urlgrabber library. This includes an HTTP handler for urllib2 that supports HTTP 1.1 and keepalive: >>> import urllib2 >>> from urlgrabber.keepalive import HTTPHandler >>> keepalive_handler = HTTPHandler() >>> opener = urllib2.build_opener(keepalive_handler) >>> urllib2.install_opener(opener) >>> >>> fo = urllib2.urlopen('http://www.python.org') Note: you should use urlgrabber version 3.9.0 or earlier, as the keepalive module has been removed in version 3.9.1

Import error: No module name urllib2

匿名 (未验证) 提交于 2019-12-03 01:29:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: Here's my code: import urllib2 . request response = urllib2 . urlopen ( "http://www.google.com" ) html = response . read () print ( html ) Any help? 回答1: As stated in the urllib2 documentation : The urllib2 module has been split across several modules in Python 3 named urllib.request and urllib.error . The 2to3 tool will automatically adapt imports when converting your sources to Python 3. So you should instead be saying from urllib . request import urlopen html = urlopen ( "http://www.google.com/" ) print ( html ) Your current,