gpt4 book ai didi

python - 将 .txt url 保存在文件夹中

转载 作者:行者123 更新时间:2023-12-04 10:49:03 24 4
gpt4 key购买 nike

我正在练习 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

最佳答案

仅使用文件名并确保 url 可访问。

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com