如何从txt文件下载图片?

How to download images from a txt file?

[![在此处输入图片描述][1]][1]我想从维基百科页面下载图片,所以我编写了这个程序,它保存了包含所有链接的 txt 文件,但我没有不知道如何继续程序下载文件。 有人可以帮助我吗?

from urllib.request import urlopen
from bs4 import BeautifulSoup
from requests import get 
import urllib.request
import wikipedia
import requests
import re

title = input("Title: ")
link = (wikipedia.page(title).url)
html = urlopen(link)
bs = BeautifulSoup(html, 'html.parser')
images = bs.find_all('img', {'src':re.compile('.jpg')})
f= open("cache.txt","w+")
for image in images: 
    url = ('https:' + image['src']+'\n')
    f.write(url)

您可以使用wget模块下载文件。

pip install wget

使用 wget 下载文件

wget.download(url)

您必须浏览 txt 文件中的每一行并使用 wget 下载文件。

python代码

import wget
import csv


with open("cache.txt","r") as f:
    line = csv.reader(f)
    for i in line:
        wget.download(i[0])

我发现这可能有帮助... 它下载一个图像,但其余的 urllib.error.HTTPError: HTTP 错误 404: 未找到

import wget
import csv
with open('cache.csv', newline='') as csvfile:
     spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
     for row in spamreader:
         wget.download(', '.join(row))

我解决了这是代码:

from urllib.request import urlopen
from bs4 import BeautifulSoup
from requests import get 
import urllib.request
import wikipedia
import requests
import re

title = input("Title: ")
link = (wikipedia.page(title).url)
html = urlopen(link)
bs = BeautifulSoup(html, 'html.parser')
images = bs.find_all('img', {'src':re.compile('.jpg')})
f= open("cache.txt","w+")
for image in images: 
    url = ('https:' + image['src']+'\n')
    f.write(url)

with open('cache.txt') as f:
   for line in f:
      url = line
      path = 'image'+url.split('/', -1)[-1]
      urllib.request.urlretrieve(url, path.rstrip('\n'))