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!