如何在 Windows 上使用 BeautifulSoup 创建 Python 脚本,以下载 WIkimedia Commons 文件夹中每张图片的最高分辨率?

How can I create a Python Script with BeautifulSoup on Windows to download the highest resolution of each picture in a WIkimedia Commons folder?

所以,我是古斯塔夫·多雷 (Gustave Doré) 的忠实粉丝,我想从整理整齐的维基共享资源文件夹下载他的所有版画。

所以,给定一个 Wikimedia Commons 文件夹,我需要以最高分辨率下载其中的所有图片

我开始写东西了,但是我写的不是很好,所以只是一个模板:

import os, requests, bs4

url = 'URL OF THE WIKIMEDIA COMMONS FOLDER'

os.makedirs('NAME OF THE FOLDER', exist_ok=True)
for n in range(NUMBER OF PICTURES IN THE PAGE - 1):
    print('I am downloading page number %s...' %(n+1))
    res = requests.get(url)
    res.raise_for_status()

    soup = bs4.BeautifulSoup(res.text, 'html.parser')

    #STUFF I STILL NEED TO ADD
    
print('Done')

例如,我会将其作为文件夹的 URL 提供:

然后我想点击每个 link 并转到图片页面,例如:

然后点击'original file'[=54=图片下方的link下载'原始文件' ]. 除了有时图片没有可用的更高分辨率,例如在这种情况下:

而且点击图片下方的link即可下载

我完全卡住了,在此先感谢您的帮助!

如果图片在保存时在其页面上注明了名称,则可加分

(例如第二个link图片应该保存为'Astonishment of the Crusaders at the Wealth of the East.jpg')

嘿,古斯塔夫·多雷 (Gustave Doré) 的忠实粉丝,这是您可以做到的方法

r = requests.get('https://commons.wikimedia.org/wiki/Category:Crusades_by_Gustave_Dor%C3%A9')
soup = BeautifulSoup(r.text, 'html.parser')
links = [i.find('img').get('src') for i in soup.find_all('a', class_='image')]
links = ['/'.join(i.split('/')[:-1]).replace('/thumb', '') for i in links]
for l in links:
    im = requests.get(l)
    with open(l.split('/')[-1], 'wb') as f:
        f.write(im.content)