How to Read From a Website With Python

One of the Python programming language's strength comes through the availability of built-in functions. Because of the variety of these functions, you can perform a variety of tasks with relative ease. For example, using the functions included in the "urllib2" module, you can pull data from a webpage to store in a variable. This way, you can do with the data whatever you wish.

Things You'll Need

  • Python Interpreter
Show More

Instructions

    • 1

      Import the "urllib2" library:

      >>>import urllib2

    • 2

      Request data from a URL through the "urlopen" function:

      >>>info = urllib2.urlopen('http://www.google.com')

    • 3

      Print the data in the variable. The variable will contain the source code for the webpage:

      >>>print info.read(89) //reads 89 characters from the source file
      html><html><head><meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">

Related Searches:

References

Comments

Related Ads

Featured