import requests
res = requests.get("https://google.com")
res.raise_for_status()
print(len(res.text))
print(res.text)
with open('mygoogle.html',"w",encoding=('utf-8')) as f :
f.write(res.text)
with open as f
部分不工作。如果有,它必须打开新文件,但它没有。
我已经测试了这段代码,它工作正常-mygoogle.html
得到编写并包含预期的内容。问题可能在剧本之外。由于它使用的是相对路径,问题可能是文件被写在你不期望的地方,因为工作目录没有设置你期望的方式- Windows通常默认为C:Windowssystem32
或你的用户配置文件目录(%userprofile%
akaC:users<username>
)为新的cmd实例。