create a python code to download all pdf files in this website…
- User
create a python code to download all pdf files in this website
https://www.beagledatabase.co.za/companies/8ovHrj/scorecards
- Assistant
You can use the requests library in Python to download the PDF files from the given website. Here's an example code that downloads all the PDF files from the provided website:
pythonimport requests from bs4 import BeautifulSoup from urllib.parse import urljoin # URL of the website url = 'https://www.beagledatabase.co.za/companies/8ovHrj/scorecards' # Send a GET request to the website response = requests.get(url) # Parse the HTML content of the response soup = BeautifulSoup(response.content, 'html.parser') # Find all <a> tags with href ending in .pdf pdf_links = soup.find_all('a', href=lambda href: href and href.endswith('.pdf')) # Download each PDF file for link in pdf_links: pdf_url = urljoin(url, link['href']) pdf_filename = pdf_url.split('/')[-1] print(f"Downloading {pdf_filename}...") # Send a GET request to the PDF URL pdf_response = requests.get(pdf_url) # Save the PDF file with open(pdf_filename, 'wb') as f: f.write(pdf_response.content) print(f"{pdf_filename} downloaded successfully!\n") print("All PDF files downloaded.") - User
include a function to download the pdf file as txt
- Assistant
Sure! Here's an updated code that includes a function to download the PDF file as text:
python - User
download each file for the desktop
- Assistant
To download the files on the desktop, you can specify the absolute path to the desktop folder in the
download_pdf_filefunction. Here's the modified code: