要在Python中爬取网页并创建文件,可以按照以下步骤进行:
import requests
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 requests
url = "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"的文件。你可以根据需要自定义文件路径和名称。