编写一个爬取httpsnewscctvcom20230707ARTIjG7nwGLOqgErR2fXtDvX230707shtmlspm=C94212PBZrLs0D62ldEKoevbmLqVHC4网站并写入一个文本中的完整python代码
以下是一个使用Python编写的爬取网站内容并写入文本的示例代码:
import requests
# 发送GET请求并获取网页内容
url = "https://news.cctv.com/2023/07/07/ARTIjG7nwGLOqgErR2fXtDvX230707.shtml?spm=C94212.PBZrLs0D62ld.EKoevbmLqVHC.4"
response = requests.get(url)
html_content = response.text
# 将网页内容写入文本文件
with open("output.txt", "w", encoding="utf-8") as file:
file.write(html_content)
这段代码使用requests库发送GET请求,获取指定网址的网页内容。然后将网页内容写入名为output.txt的文本文件中。请确保你已经安装了requests库,可以使用pip install requests进行安装。
原文地址: http://www.cveoy.top/t/topic/hQpS 著作权归作者所有。请勿转载和采集!