Я создал код для загрузки бесплатного pdf страницы, поэтому я не делаю это вручную, как показано в коде, который я прикрепляю для определенной подмножества (например, https: // www ... ....... / MagPi01).
Теперь я хочу расширить код, чтобы получить каждую ссылку после главной страницы, содержащую pdf не только в формате «MagPi {}. Format (1,2,3 ...), но и (. *) С регулярным выражением.
Я пробую это таким образом (регулярное выражение) в образовательных целях.
import urllib.request
import os
path = "C:/Users/kosmas/Desktop/MagPi"
try:
os.mkdir(path)
except OSError:
print ("Creation of the directory %s failed" % path)
else:
print ("Successfully created the directory %s " % path)
try:
i = 1
while i < 10:
url = "https://www.raspberrypi.org/magpi-issues/MagPi0{}.pdf".format(i)
opener = urllib.request.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
urllib.request.install_opener(opener)
urllib.request.urlretrieve(url, "C:/Users/kosmas/Desktop/MagPi/MagPi0{}.pdf".format(i))
print("MagPi0{}.pdf created successfully".format(i))
i = i + 1
except:
print('Something went wrong')
try:
i = 10
while i < 82:
url = "https://www.raspberrypi.org/magpi-issues/MagPi{}.pdf".format(i)
opener = urllib.request.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
urllib.request.install_opener(opener)
urllib.request.urlretrieve(url, "C:/Users/kosmas/Desktop/MagPi/MagPi{}.pdf".format(i))
print("MagPi{}.pdf created successfully".format(i))
i = i + 1
except:
print('Something went wrong')