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

Source: Internet
Author: User
This article mainly introduces Python3's method of capturing and saving the webpage source code using the requests package. The example analyzes the related use skills of the requests module in the Python3 environment, for more information about how to use the requests package in Python3 to capture and save the webpage source code, see the example in this article. 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)

For more information about how to use the requests package in Python3 to capture and save the webpage source code, see The PHP Chinese website!

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.