Master Web Scraping in Python with urllib2: A Step‑by‑Step Guide
This article explains how to use Python's urllib2 module to fetch web pages, covering basic request handling, creating Request objects, sending GET and POST data, setting custom headers, and demonstrates practical examples with code snippets and screenshots.
Web scraping means reading network resources specified by a URL from the web stream and saving them locally, similar to simulating a browser's HTTP request and reading the server's response.
In Python, the urllib2 module provides a simple interface for fetching web pages via the urlopen function.
The simplest urllib2 program consists of four lines. A file named urllib2_test01.py demonstrates this basic usage:
Running the script (press F5) prints the HTML source of the Baidu homepage, which matches the source viewed via a browser's "View Page Source" feature.
Besides the http: scheme, urllib2 also supports ftp:, file:, and other protocols.
urllib2 uses a Request object to represent an HTTP request. By creating a Request with the target URL and passing it to urlopen, you receive a response object that behaves like a file, allowing you to call .read() to obtain the content.
A second example in urllib2_test02.py shows the same output as the first script:
urllib2 handles all URL schemes with the same interface, allowing you to create an FTP request in a similar way:
When making HTTP requests, you can also send additional data. POST requests are commonly used to submit form data, but you can POST any data to your own programs. Data must be URL‑encoded using functions from the urllib module (not urllib2) before being passed as the data parameter of the Request object.
An example in urllib2_test03.py demonstrates sending data:
If no data parameter is provided, urllib2 performs a GET request. GET and POST differ mainly in side effects: POST typically changes server state, while GET can also transmit data by encoding it in the URL.
Data can also be sent via a GET request by encoding it directly in the URL, as shown:
Some sites dislike programmatic access or serve different content based on the client. By default, urllib2 identifies itself as "Python-urllib/x.y". You can customize the User-Agent header by providing a headers dictionary when constructing the Request object, for example to mimic Internet Explorer.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
