Skip to content Skip to sidebar Skip to footer

How To Send A Urllib2 Request With Added White Spaces

I am trying to send a request to open web page url that uses white spaces so that I can download a file from the page. In a normal browser i.e chrome when you enter the url into t

Solution 1:

In order to clean your url with whitespaces use urllib.quote like this:

importurlliburl= urllib.quote("http://www.example.com/a url with whitespaces")

To download a file to cannot use functions like urllib2.urlopen. If you want to download a file using the urllib modules you need urllib.urlretrieve. However, requests is easier to grasp in the beginning.

importrequestsresponse= requests.get(url)

The response provides several useful functions:

  • response.text: The source code of the website or the content of the downloaded file.
  • response.status_code: Status code of your request. 200 is ok.

You probably want to save your downloaded file somewhere. So open a file connection with open in binary mode and write the content of your response. Do not forget to close the file.

your_file_connection = open('your_file', 'wb')
your_file_connection.save(response.text)
your_file_connection.flush()
your_file_connection.close()

Summary

import urllib
import requests

url = urllib.quote("http://www.example.com/a url with whitespaces")
response = requests.get(url)

your_file_connection = open('your_file', 'wb')
your_file_connection.save(response.text)
your_file_connection.
your_file_connection.close()

requests Documentation: http://docs.python-requests.org/en/latest/

Solution 2:

While the answer of Jon was then the correct way, note that in Python 3.X you have to change it to:

import urllib.parse
url = urllib.parse.quote("http://www.example.com/a url with whitespaces"')

Solution 3:

After attempting this, I figured out that the line: your_file_connection.save(response.content)

needs to be: your_file_connection.write(response.content)

at least on Python 2.*

Post a Comment for "How To Send A Urllib2 Request With Added White Spaces"