Urllib crawler Development in "Python" Python3

Source: Internet
Author: User

Here are three ways

①first Method

The simplest way

② Add Data,http Header

Using the Request object

③cookiejar
Importurllib.request fromhttpImportCookiejarurl='http://www.baidu.com'Print("First Method") Response1=urllib.request.urlopen (URL)#Return Status CodePrint(Response1.getcode ())Print(Len (Response1.read ()))Print("Second Method") Request=urllib.request.Request (URL) request.add_header ("uese-agent","mazilla/5.0") Response2=urllib.request.urlopen (URL)#Return Status CodePrint(Response2.getcode ())Print(Len (Response2.read ()))Print("Third Method")#declaring a Cookiejar object instance to hold a cookieCJ =Cookiejar. Cookiejar ()#using the Httpcookieprocessor object of the Urllib.request library to create a cookie processor, it is also CookiehandlerHandler =Urllib.request.HTTPCookieProcessor (CJ)#Create opener with CookiehandlerOpener =Urllib.request.build_opener (handler)#The Open method here is the same as Urllib.request's Urlopen method, which can also be passed to the requestRESPONSE3 =opener.open (URL)#Return Status CodePrint(Response3.getcode ())Print(Response3.read ())

Urllib crawler Development in "Python" Python3

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.