Python3 uses the requests package to capture and save the webpage source code.

Source: Internet
Author: User

Python3 uses the requests package to capture and save the webpage source code.

This example describes how Python3 uses the requests package to capture and save the web page source code. We will share this with you for your reference. The details are as follows:

Use the requests module of Python 3 to capture the webpage source code and save it to the file example:

Import requestshtml = requests. get ("http://www.baidu.com") with open('test.txt ', 'w', encoding = 'utf-8') as f: f. write (html. text)

This is a basic file storage operation, but there are several noteworthy issues:

1. install the requests package. Run pip install requests on the command line to automatically install the package. Many people recommend using requests. The built-in urllib. request can also capture the webpage source code.

2. Set the encoding parameter of the open Method to UTF-8. Otherwise, garbled characters will appear in the saved file.

3. If the captured content is output directly in cmd, various encoding errors will be prompted, so save it to the file for viewing.

4. The with open method is a better way to release resources after automatic operations are completed.

Another example:

Import requestsff = open('testt.txt ', 'w', encoding = 'utf-8') with open('test.txt', encoding = "UTF-8") as f: for line in f: ff. write (line) ff. close ()

This is an example of reading a txt file, reading a row each time, and saving it to another txt file.

Because the data of each row read is printed in the command line, an Encoding Error occurs in Chinese. Therefore, each row is read and saved to another file to test whether the reading is normal. (Encoding method should be set when opening)

Related Article

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.