1, Python crawler request.urlopen request for Web Access to the source code

Source: Internet
Author: User

# Python3 Import Request Package from Urllib ImportRequestImport SYSImport io# If you need print printing, you can set the output environment first if an exception occursSys.StdOut=Io.Textiowrapper (SYS.StdOut.Buffer, encoding=' Utf-8 ')# The URL you need to getUrl= ' http://www.xxx.com/'# header FileHeaders={"User-agent":"mozilla/5.0 (Windows NT 10.0; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/64.0.3282.186 safari/537.36 "}# Generate Request ObjectReq=Request.Request (URL, headers=Headers# Call the request's Urlopen method to initiate the demand and return the result object, or a GET request if there is no data parameter, or the POST requestResponse=Request.Urlopen (req)# Write the results into an HTML file, with Open(' a.html ',' WB ') asF:f.Write (response.Read ())# Print the returned status codePrint(Response.GetCode ())# Print the returned URL to prevent redirection of URL changesPrint(Response.Url

1, Python crawler request.urlopen request the Web page to obtain the source code

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.