Xml FileNotFoundError:[Errno 2]:带有open()的变量文件名
我正在用Python开发一个webscraper,它从xml文件中获取名称和下载链接。首先,它连接了date的名字hof和az。然后,它应该下载ziplink后面的文件(www.file.io/xyzfile.zip),并将其保存在同一目录中的连接名称下 但它不会使用我的连接名称。因此,一般的问题是:我需要提供什么确切的信息作为函数的参数?对于type(),我确保提供一个字符串,但它不会接受Xml FileNotFoundError:[Errno 2]:带有open()的变量文件名,xml,python-3.x,web-scraping,io,Xml,Python 3.x,Web Scraping,Io,我正在用Python开发一个webscraper,它从xml文件中获取名称和下载链接。首先,它连接了date的名字hof和az。然后,它应该下载ziplink后面的文件(www.file.io/xyzfile.zip),并将其保存在同一目录中的连接名称下 但它不会使用我的连接名称。因此,一般的问题是:我需要提供什么确切的信息作为函数的参数?对于type(),我确保提供一个字符串,但它不会接受 import requests from bs4 import BeautifulSoup xml =
import requests
from bs4 import BeautifulSoup
xml = requests.get('https://www.rechtsprechung-im-internet.de/rii-toc.xml')
soup = BeautifulSoup(xml.text, 'xml')
for item in soup.find_all('item'):
ziplink=str(item.link.text)
datum=str(item.find('entsch-datum').text)
az=str(item.aktenzeichen.text)
hof=str(item.gericht.text)
name=datum+'-'+hof+'-'+az
print(type(name))
r=requests.get(ziplink, allow_redirects=True)
with open('%s.zip' % name,'wb') as f:
f.write(r.content)
print(name)
然而,不幸的是,我得到了以下错误:
Traceback (most recent call last):
File "simple_script.py", line 26, in <module>
with open('%s.zip' % name,'wb') as f:
FileNotFoundError: [Errno 2] No such file or directory: '20100114-BGH 9. Zivilsenat-IX ZB 72/08.zip'
回溯(最近一次呼叫最后一次):
文件“simple_script.py”,第26行,在
以open(“%s.zip”%name,'wb')作为f:
FileNotFoundError:[Errno 2]没有这样的文件或目录:'20100114-BGH 9。Zivilsenat IX ZB 72/08邮编'
对于print(type()),我确保提供一个字符串作为name参数。因为当我用name='test.zip'
测试代码时,它工作得很好。但是,理想情况下,我希望动态命名这些文件
这是我关于Stackoverflow的第一篇文章,我很想得到一些反馈。非常感谢你
干杯,贾斯珀您是否尝试过在打开功能中使用“wb+”权限
with open('%s.zip' % name,'wb+') as f:
f.write(r.content)
+表示如果找不到该文件,则应创建该文件