What is Urllib
Urllib is a python built-in HTTP request library
Includes the following modules
Urllib.request Request Module
Urllib.error Exception Handling Module
Urllib.parse URL Parsing module
Urllib.robotparser robots.txt Parsing Module
Urlopen
Introduction to Urllib.request.urlopen Parameters:
Urllib.request.urlopen (URL, data=none, [Timeout,]*, Cafile=none, Capath=none, Cadefault=false, Context=none)
Use of URL parameters
Let's write a simple example:
Import urllib.requestresponse = Urllib.request.urlopen ('http://www.baidu.com')print ( Response.read (). Decode ('utf-8'))
Urlopen commonly used has three parameters, its parameters are as follows:
Urllib.requeset.urlopen (Url,data,timeout)
Response.read () can get the contents of the Web page, and if there is no read (), the following will be returned
Use of the data parameter
The above example is by requesting Baidu's get request to get Baidu, the following use Urllib POST request
Here through the Http://httpbin.org/post website demo (the site can be used as a site for practice using Urllib, you can
Simulate various request operations).
import Urllib.parse Import Urllib.requestdata = Bytes (Urllib.parse.urlencode ({ "word": ' hello"}), Encoding= ' utf8" ) print ' http://httpbin.org/post", Data=data) print (Response.read ())
Python Crawler II Basic use of Urllib library