我正在练习 python,我有一个包含 url 的列表,我需要转到该 url 并将页面内容保存在 save_folder 中
list_name = ['www.xyz.com/abc/sample1.txt','www.xyz.com/abc/sample2.txt','www.xyz.com/abc/sample44.txt']
for i in list_name:
http = urllib3.PoolManager()
r = http.request('get', i)
with open('save_folder/' + i,'w') as f:
f.write(r.data)
f.close
当我运行上述代码时,出现错误 No such file or directory: 'save_folder/www.xyz.com/abc/sample1.txt
目标是读取列表中的三个项目,并使用 urllib3 转到列表中的每个页面,并将每个页面保存在 save_folder 中,文件名为 sample1.txt,sample2.txt,sample44.txt
最佳答案
仅使用文件名并确保网址可访问。
for i in list_name:
http = urllib3.PoolManager()
r = http.request('get', i)
fname = i.split("/")[-1]
with open('./save_folder/' + fname,'wb') as f:
f.write(r.data)
f.close
关于python - 将 .txt url 保存在文件夹中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59572466/