Questions tagged [urllib2]

urllib2 is a builtin python 2 module that defines functions and classes to help with URL actions. It is notably unsatisfactory and has been replaced in python 3 and by third-party libraries.

urllib2 is a module that defines functions and classes to help with URL actions (basic and digest authentication, redirections, cookies, etc). It supersedes urllib in Python 2, and in python 3 has been superseded by a new library called urllib.

In addition, the requests third party module has become a de facto standard to accomplish the same tasks.

2960 questions
1005
votes
11 answers

What are the differences between the urllib, urllib2, urllib3 and requests module?

In Python, what are the differences between the urllib, urllib2, urllib3 and requests modules? Why are there three? They seem to do the same thing...
Paul Biggar
  • 27,579
  • 21
  • 99
  • 152
570
votes
10 answers

Import error: No module name urllib2

Here's my code: import urllib2.request response = urllib2.urlopen("http://www.google.com") html = response.read() print(html) Any help?
delete
503
votes
18 answers

How to download image using requests

I'm trying to download and save an image from the web using python's requests module. Here is the (working) code I used: img = urllib2.urlopen(settings.STATICMAP_URL.format(**data)) with open(path, 'w') as f: f.write(img.read()) Here is the new…
shkschneider
  • 17,833
  • 13
  • 59
  • 112
222
votes
20 answers

Downloading a picture via urllib and python

So I'm trying to make a Python script that downloads webcomics and puts them in a folder on my desktop. I've found a few similar programs on here that do something similar, but nothing quite like what I need. The one that I found most similar is…
Mike
  • 2,247
  • 2
  • 14
  • 5
219
votes
10 answers

Why do I get "'str' object has no attribute 'read'" when trying to use `json.load` on a string?

In Python I'm getting an error: Exception: (, AttributeError("'str' object has no attribute 'read'",), ) Given python code: def getEntries (self, sub): url =…
RobinJ
  • 5,022
  • 7
  • 32
  • 61
162
votes
4 answers

How to convert a dictionary to query string in Python?

After using cgi.parse_qs(), how to convert the result (dictionary) back to query string? Looking for something similar to urllib.urlencode().
Sam
  • 4,727
  • 9
  • 23
  • 17
135
votes
3 answers

Need to install urllib2 for Python 3.5.1

I'm running Python 3.5.1 for Mac. I want to use urllib2 module. I tried installing it but I was told that it's been split into urllib.request and urllib.error for Python 3. My command (running from the framework bin directory for now because it's…
Eamonn Gormley
  • 2,436
  • 4
  • 21
  • 21
112
votes
4 answers

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

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): …
Athena Wisdom
  • 6,101
  • 9
  • 36
  • 60
107
votes
15 answers

Python: download files from google drive using url

I am trying to download files from google drive and all I have is the drive's URL. I have read about google API that talks about some drive_service and MedioIO, which also requires some credentials( mainly JSON file/OAuth). But I am unable to get…
rkatkam
  • 2,634
  • 3
  • 18
  • 29
101
votes
9 answers

Changing user agent on urllib2.urlopen

How can I download a webpage with a user agent other than the default one on urllib2.urlopen? urllib2.urlopen is not available in Python 3.x; the 3.x equivalent is urllib.request.urlopen. See Changing User Agent in Python 3 for…
das
98
votes
10 answers

Python urllib2: Receive JSON response from url

I am trying to GET a URL using Python and the response is JSON. However, when I run import urllib2 response = urllib2.urlopen('https://api.instagram.com/v1/tags/pizza/media/XXXXXX') html=response.read() print html The html is of type str and I am…
Deepak B
  • 2,245
  • 5
  • 26
  • 28
96
votes
4 answers

How do I catch a specific HTTP error in Python?

I have import urllib2 try: urllib2.urlopen("some url") except urllib2.HTTPError: but what I end up is catching any kind of HTTP error. I want to catch only if the specified webpage doesn't exist (404?).
Arnab Sen Gupta
  • 5,639
  • 5
  • 24
  • 17
87
votes
3 answers

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

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.
Varada
  • 871
  • 1
  • 6
  • 4
84
votes
5 answers

python: urllib2 how to send cookie with urlopen request

I am trying to use urllib2 to open url and to send specific cookie text to the server. E.g. I want to open site Solve chess problems, with a specific cookie, e.g. search=1. How do I do it? I am trying to do the following: import urllib2 (need to…
Oleg Tarasenko
  • 9,324
  • 18
  • 73
  • 102
83
votes
4 answers

Python3 error: initial_value must be str or None, with StringIO

While porting code from python2 to 3, I get this error when reading from a URL TypeError: initial_value must be str or None, not bytes. import urllib import json import gzip from urllib.parse import urlencode from urllib.request import…
AMisra
  • 1,869
  • 2
  • 25
  • 45
1
2 3
99 100