Python Programming/Internet

From Wikibooks, open books for an open world
Jump to navigation Jump to search

The urllib module which is bundled with python can be used for web interaction. This module provides a file-like interface for web urls.

Getting page text as a string[edit | edit source]

An example of reading the contents of a webpage

import urllib.request as urllib
pageText = urllib.urlopen("").read()

Processing page text line by line:

import urllib.request as urllib
for line in urllib.urlopen(""):

Get and post methods can be used, too.

import urllib.request as urllib
params = urllib.urlencode({"plato":1, "socrates":10, "sophokles":4, "arkhimedes":11})

# Using GET method
pageText = urllib.urlopen("" % params).read()

# Using POST method
pageText = urllib.urlopen("", params).read()

Downloading files[edit | edit source]

To save the content of a page on the internet directly to a file, you can read() it and save it as a string to a file object

import urllib2
data = urllib2.urlopen("", "pythonbook.pdf").read() # not recommended as if you are downloading 1gb+ file, will store all data in ram.
file =  open('Python_Programming.pdf','wb')

This will download the file from here and save it to a file "pythonbook.pdf" on your hard drive.

Other functions[edit | edit source]

The urllib module includes other functions that may be helpful when writing programs that use the internet:

>>> plain_text = "This isn't suitable for putting in a URL"
>>> print(urllib.quote(plain_text))
>>> print(urllib.quote_plus(plain_text))

The urlencode function, described above converts a dictionary of key-value pairs into a query string to pass to a URL, the quote and quote_plus functions encode normal strings. The quote_plus function uses plus signs for spaces, for use in submitting data for form fields. The unquote and unquote_plus functions do the reverse, converting urlencoded text to plain text.

Email[edit | edit source]

With Python, MIME compatible emails can be sent. This requires an installed SMTP server.

import smtplib
from email.mime.text import MIMEText

msg = MIMEText( 
"""Hi there,

This is a test email message.


me  = ''
you = ''
msg['Subject'] = 'Hello!'
msg['From'] =  me
msg['To'] =  you
s = smtplib.SMTP()
s.sendmail(me, [you], msg.as_string())

This sends the sample message from '' to ''.

External links[edit | edit source]