Python Crawler II Basic use of Urllib library

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.