要在Python中爬取網(wǎng)頁并創(chuàng)建文件,可以按照以下步驟進(jìn)行:
import requests
url = "https://www.example.com" # 替換成要爬取的網(wǎng)頁鏈接
response = requests.get(url)
if response.status_code == 200:
# 繼續(xù)處理響應(yīng)內(nèi)容
else:
print("請(qǐng)求失敗")
file_path = "output.html" # 替換成要?jiǎng)?chuàng)建的文件路徑和名稱
with open(file_path, "w", encoding="utf-8") as file:
file.write(response.text)
完整的代碼示例:
import requests
url = "https://www.example.com" # 替換成要爬取的網(wǎng)頁鏈接
response = requests.get(url)
if response.status_code == 200:
file_path = "output.html" # 替換成要?jiǎng)?chuàng)建的文件路徑和名稱
with open(file_path, "w", encoding="utf-8") as file:
file.write(response.text)
print("文件創(chuàng)建成功")
else:
print("請(qǐng)求失敗")
此代碼將爬取指定網(wǎng)頁的內(nèi)容,并將內(nèi)容保存為一個(gè)名為"output.html"的文件。你可以根據(jù)需要自定義文件路徑和名稱。