【问题标题】:Python script to save webpage and rename it while saving (save as - command)保存网页并在保存时重命名的Python脚本(另存为-命令)
【发布时间】:2014-06-19 20:50:17
【问题描述】:

您好,我搜索了很多,最后没有关于如何使用 python 2.6 保存网页并在保存时重命名的相关结果。

【问题讨论】:

  • 在谷歌中搜索。 "python urllib 获取内容写入文件"

标签: python html python-2.7


【解决方案1】:

更好的用户请求库:

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)

【讨论】:

    【解决方案2】:

    您可能希望使用 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)
    

    【讨论】:

      猜你喜欢
      • 2019-05-12
      • 2016-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-10
      • 2020-02-13
      • 2013-07-02
      • 1970-01-01
      相关资源
      最近更新 更多