urllib2 is a module that comes with Python2.7 (no need to download, just import it and use it).
Basic use of the urllib2 library
The so-called web page crawling is to read the network resources specified in the URL address from the network stream and save them locally. There are many libraries in Python that can be used to crawl web pages. Let’s learn urllib2 first.
urllib2 是 Python2.7 自带的模块(不需要下载,导入即可使用) urllib2 官方文档:https://docs.python.org/2/library/urllib2.html urllib2 源码:https://hg.python.org/cpython/file/2.7/Lib/urllib2.py
urllib2 was changed to urllib.request in python3.x
urlopen
Let’s start with a piece of code:
# urllib2_urlopen.py # 导入urllib2 库 import urllib2 # 向指定的url发送请求,并返回服务器响应的类文件对象 response = urllib2.urlopen("http://www.baidu.com") # 类文件对象支持 文件对象的操作方法,如read()方法读取文件全部内容,返回字符串 html = response.read() # 打印字符串 print html
Execute the python code written and the results will be printed
Power@PowerMac ~$: python urllib2_urlopen.py
In fact, if we open the Baidu homepage on the browser, right-click and select "View Source Code", you will find that it is exactly the same as what we just printed . In other words, the above 4 lines of code have helped us crawl down all the code on Baidu's homepage.
The python code corresponding to a basic url request is really very simple.
Request
In our first example, the parameter of urlopen() is a url address;
But if you need to perform more complex Operations, such as adding HTTP headers, must create a Request instance as a parameter of urlopen(); and the URL address that needs to be accessed is used as a parameter of the Request instance.
We edit urllib2_request.py
# urllib2_request.py import urllib2 # url 作为Request()方法的参数,构造并返回一个Request对象 request = urllib2.Request("http://www.baidu.com") # Request对象作为urlopen()方法的参数,发送给服务器并接收响应 response = urllib2.urlopen(request) html = response.read() print html
The running result is exactly the same:
新建Request实例,除了必须要有 url 参数之外,还可以设置另外两个参数: data(默认空):是伴随 url 提交的数据(比如要post的数据),同时 HTTP 请求将从 "GET"方式 改为 "POST"方式。 headers(默认空):是一个字典,包含了需要发送的HTTP报头的键值对。 这两个参数下面会说到。
User-Agent
But this is direct Using urllib2 to send a request to a website is indeed a bit abrupt. Just like every house has a door, it is obviously not very polite for you to barge in directly as a passerby. Moreover, some sites do not like to be visited by programs (non-human visits) and may deny your access request.
But if we use a legal identity to request other people's websites, they will obviously welcome it, so we should add an identity to our code, which is the so-called User-Agent header.
The browser is a recognized and allowed identity in the Internet world. If we want our crawler program to be more like a real user, then our first step is to pretend to be a recognized browser. . Different browsers will have different User-Agent headers when sending requests. The default User-Agent header of urllib2 is: Python-urllib/x.y (x and y are the Python major and minor version numbers, such as Python-urllib/2.7)
#urllib2_useragent.py import urllib2 url = "http://www.itcast.cn" #IE 9.0 的 User-Agent,包含在 ua_header里 ua_header = {"User-Agent" : "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0;"} # url 连同 headers,一起构造Request请求,这个请求将附带 IE9.0 浏览器的User-Agent request = urllib2.Request(url, headers = ua_header) # 向服务器发送这个请求 response = urllib2.urlopen(request) html = response.read() print html
Add more Header information
Add a specific Header to the HTTP Request to construct a complete HTTP request message.
You can add/modify a specific header by calling Request.add_header() or view existing headers by calling Request.get_header().
Add a specific header
# urllib2_headers.py import urllib2 url = "http://www.itcast.cn" #IE 9.0 的 User-Agent header = {"User-Agent" : "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0;"} request = urllib2.Request(url, headers = header) #也可以通过调用Request.add_header() 添加/修改一个特定的header request.add_header("Connection", "keep-alive") # 也可以通过调用Request.get_header()来查看header信息 # request.get_header(header_name="Connection") response = urllib2.urlopen(req) print response.code #可以查看响应状态码 html = response.read() print html
Randomly add/modify User-Agent
# urllib2_add_headers.py import urllib2 import random url = "http://www.itcast.cn" ua_list = [ "Mozilla/5.0 (Windows NT 6.1; ) Apple.... ", "Mozilla/5.0 (X11; CrOS i686 2268.111.0)... ", "Mozilla/5.0 (Macintosh; U; PPC Mac OS X.... ", "Mozilla/5.0 (Macintosh; Intel Mac OS... " ] user_agent = random.choice(ua_list) request = urllib2.Request(url) #也可以通过调用Request.add_header() 添加/修改一个特定的header request.add_header("User-Agent", user_agent) # 第一个字母大写,后面的全部小写 request.get_header("User-agent") response = urllib2.urlopen(req) html = response.read() print html
Related tutorial recommendations :Python video tutorial
The above is the detailed content of How to install the urllib2 library in Python. For more information, please follow other related articles on the PHP Chinese website!

Python's flexibility is reflected in multi-paradigm support and dynamic type systems, while ease of use comes from a simple syntax and rich standard library. 1. Flexibility: Supports object-oriented, functional and procedural programming, and dynamic type systems improve development efficiency. 2. Ease of use: The grammar is close to natural language, the standard library covers a wide range of functions, and simplifies the development process.

Python is highly favored for its simplicity and power, suitable for all needs from beginners to advanced developers. Its versatility is reflected in: 1) Easy to learn and use, simple syntax; 2) Rich libraries and frameworks, such as NumPy, Pandas, etc.; 3) Cross-platform support, which can be run on a variety of operating systems; 4) Suitable for scripting and automation tasks to improve work efficiency.

Yes, learn Python in two hours a day. 1. Develop a reasonable study plan, 2. Select the right learning resources, 3. Consolidate the knowledge learned through practice. These steps can help you master Python in a short time.

Python is suitable for rapid development and data processing, while C is suitable for high performance and underlying control. 1) Python is easy to use, with concise syntax, and is suitable for data science and web development. 2) C has high performance and accurate control, and is often used in gaming and system programming.

The time required to learn Python varies from person to person, mainly influenced by previous programming experience, learning motivation, learning resources and methods, and learning rhythm. Set realistic learning goals and learn best through practical projects.

Python excels in automation, scripting, and task management. 1) Automation: File backup is realized through standard libraries such as os and shutil. 2) Script writing: Use the psutil library to monitor system resources. 3) Task management: Use the schedule library to schedule tasks. Python's ease of use and rich library support makes it the preferred tool in these areas.

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

WebStorm Mac version
Useful JavaScript development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version
Recommended: Win version, supports code prompts!

Zend Studio 13.0.1
Powerful PHP integrated development environment