【问题标题】:Unable to use URL as filename in python无法在 python 中使用 URL 作为文件名
【发布时间】:2014-10-17 19:17:29
【问题描述】:

我正在尝试使用 url 作为从我的 python 脚本生成的文件的文件名。 但是当我尝试这样做时,它会返回以下错误:

Traceback (most recent call last):
File "webdriver_adhoc_crawler1.py", line 36, in <module>
fo = io.open('%s' % line, 'w+', encoding='utf8')
IOError: [Errno 2] No such file or directory: 'http://www.google.com'

错误发生在这一行:

line="http://www.google.com"
fo = io.open('%s' % line, 'w+', encoding='utf8')

我该如何解决这个问题?或者甚至可以在 Linux 中使用 url 作为文件名?

【问题讨论】:

    标签: python url file-io io


    【解决方案1】:

    文件名不能包含这些字符:\/:*?"&lt;&gt;|,因此您的程序无法找到/创建此文件,并且会引发错误。

    【讨论】:

      【解决方案2】:

      如果要打开网页,可以使用“urllib2”获取网页。

      import urllib2
      response = urllib2.urlopen('http://www.google.com')
      html = response.read()
      

      【讨论】:

        猜你喜欢
        • 2021-06-29
        • 1970-01-01
        • 2022-01-18
        • 2018-09-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多