if you want to do it quick and dirty so to say do it like @Sharku sugessted and make a list
BASE_URL = 'https://www.nzx.com/announcements/'
pdf_list = ['190008', '372918', '372980']
for pdf in pdf_list:
grab = requests.get(f'{BASE_URL}{pdf}')
soup = BeautifulSoup(grab.text, 'html.parser')
# opening a file in write mode
# note: w creates a file and replaces the content,
# to append to a file use r+ if the file exist
# or a if you want to create the file and append
# to it.
f = open("t.txt", "a")
# traverse paragraphs from soup
for link in soup.find_all("a"):
data = link.get('href')
f.write(data)
f.write("\n")
f.close()***```
of course you could do more in refactoring this all in functions or use a context manager for the writing in the file but the approach should get you started.
Update:
small change so you use a conttext manager for the file and only greps the links with a .pdf extention.
import requests
from bs4 import BeautifulSoup
BASE_URL = 'https://www.nzx.com/announcements/'
pdf_list = ['190008', '372918', '372980']
with open("t.txt", "a") as f:
for pdf in pdf_list:
grab = requests.get(f'{BASE_URL}{pdf}')
soup = BeautifulSoup(grab.text, 'html.parser')
for link in soup.find_all("a"):
data = link.get('href')
if data.endswith('.pdf'):
f.write(data)
f.write("\n")
Update 2:
you can also extrat the pdf right away , i leave the t.txt file in this example but it isnt needed if you simply want to download the pdfs
import requests
from bs4 import BeautifulSoup
BASE_URL = 'https://www.nzx.com/announcements/'
pdf_list = ['190008', '372918', '372980']
with open("t.txt", "a") as f:
for pdf in pdf_list:
grab = requests.get(f'{BASE_URL}{pdf}')
soup = BeautifulSoup(grab.text, 'html.parser')
for link in soup.find_all("a"):
data = link.get('href')
if data.endswith('.pdf'):
fname = data.split('/')[-1]
pdf = requests.get(data)
with open(fname, "wb") as pf:
pf.write(pdf.content)
f.write(data)
f.write("\n")