使用Os模块创建新文件

2024-06-25 06:28:03 发布

您现在位置:Python中文网/ 问答频道 /正文

知道答案的人

import os

使用这段代码创建一个文件,这个方法将在while循环中使用

def get_data(self,url):
            pagina = urllib.request.urlopen(self.url)
            data = BeautifulSoup(pagina, "html.parser")
            dest = "C:\\Users\\Dennis.Pieruschka\\Documents\\Scraper\\Links"
            html = ".html"
            brackets = "\\"
            string = dest + brackets + url + html
            with open(string, 'w') as f:
                f.write(data)
                f.close()

    Somehow i cant parse in the name of the URL and use it to name                        the file 
Somebody knows how to fix it i get this error 

    OSError: [Errno 22] Invalid argument: 'C:\\Users\\Dennis.Pieruschka\\Documents\\Scraper\\Links\\http://www.visservanbaars.nl/vacatures/senior-oracle-dba-osb-weblogic/.html'

Tags: theselfurldatagethtmllinksscraper
2条回答

问题是:文件名中不能有:和/将被解释为目录分隔符。 你可以这样做

encoded_url = url[5:] #remove 'http:
encoded_url = encoded_url.replace('/','-' # replace / with -

然而,更好的解决方案是对所有特殊字符进行编码 导入urllib.parse encoded_url=urllib.parse.quote(url)

在Windows中,文件名上不能有:或\或/

相关问题 更多 >