要在Python中爬取网页并创建文件,可以按照以下步骤进行:
导入必要的库:import requests发送GET请求以获取网页内容:url = "https://www.example.com" # 替换成要爬取的网页链接response = requests.get(url)检查响应状态码,确保请求成功:if response.status_code == 200:# 继续处理响应内容else:print("请求失败")创建文件并将网页内容写入文件:file_path = "output.html" # 替换成要创建的文件路径和名称with open(file_path, "w", encoding="utf-8") as file:file.write(response.text)完整的代码示例:
import requestsurl = "https://www.example.com" # 替换成要爬取的网页链接response = requests.get(url)if response.status_code == 200:file_path = "output.html" # 替换成要创建的文件路径和名称with open(file_path, "w", encoding="utf-8") as file:file.write(response.text)print("文件创建成功")else:print("请求失败")此代码将爬取指定网页的内容,并将内容保存为一个名为"output.html"的文件。你可以根据需要自定义文件路径和名称。

