【发布时间】:2014-06-19 20:50:17
【问题描述】:
您好,我搜索了很多,最后没有关于如何使用 python 2.6 保存网页并在保存时重命名的相关结果。
【问题讨论】:
-
在谷歌中搜索。 "python urllib 获取内容写入文件"
标签: python html python-2.7
您好,我搜索了很多,最后没有关于如何使用 python 2.6 保存网页并在保存时重命名的相关结果。
【问题讨论】:
标签: python html python-2.7
更好的用户请求库:
import requests
pagelink = "http://www.example.com"
page = requests.get(pagelink)
with open('/path/to/file/example.html', "w") as file:
file.write(page.text)
【讨论】:
您可能希望使用 urllib(2) 包访问网页,然后将文件对象保存到所需位置(os.path)。
它应该看起来像这样:
import urllib2, os
pagelink = "http://www.example.com"
page = urllib2.urlopen(pagelink)
with open(os.path.join('/(full)path/to/Documents',pagelink), "w") as file:
file.write(page)
【讨论】: