获取豆瓣网络源码,为什么
# 准备请求数据
url = 'https://movie.douban.com/top250'
headers = {
'User-Agents': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Mobile Safari/537.36 Edg/127.0.0.0'
}
# 发送请求
response = requests.get(url, headers=headers)
print(response.status_code)
# 获取豆瓣网页源码
with open('douban.html', 'w', encoding='utf-8') as f:
f.write(response.content.decode())
状态码为418,而
import requests
url = 'https://movie.douban.com/top250'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0'
}
try:
# 发送请求
response = requests.get(url, headers=headers)
if response.status_code == 418:
print("服务器返回 418 状态码,这可能是一个玩笑或者配置问题。")
elif response.status_code == 200:
# 获取豆瓣网页源码
with open('douban.html', 'w', encoding='utf-8') as f:
f.write(response.content.decode())
else:
print(f"请求失败,状态码:{response.status_code}")
except Exception as e:
print(f"发生错误:{e}")
状态码为200成功获取源码