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:
import 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.")Make sure to install the required libraries using
pip install requests,pip install beautifulsoup4. You can find the downloaded PDF files in the same directory where you run the script. - 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:
import requests from bs4 import BeautifulSoup from urllib.parse import urljoin import PyPDF2 def download_pdf_file(url, filename): # Send a GET request to the PDF URL response = requests.get(url) with open(filename, 'wb') as f: f.write(response.content) print(f"{filename} downloaded successfully!\n") def extract_text_from_pdf(filename): with open(filename, 'rb') as f: pdf_reader = PyPDF2.PdfFileReader(f) text = "" for page_num in range(pdf_reader.numPages): page = pdf_reader.getPage(page_num) text += page.extract_text() return text # 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 and also extract text from it for link in pdf_links: pdf_url = urljoin(url, link['href']) pdf_filename = pdf_url.split('/')[-1] txt_filename = pdf_filename.replace(".pdf", ".txt") print(f"Downloading {pdf_filename}...") download_pdf_file(pdf_url, pdf_filename) print(f"Extracting text from {pdf_filename}...") pdf_text = extract_text_from_pdf(pdf_filename) with open(txt_filename, 'w', encoding='utf-8') as f: f.write(pdf_text) print(f"{txt_filename} created successfully!\n") print("All PDF files downloaded as text.") - 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:import os import requests from bs4 import BeautifulSoup from urllib.parse import urljoin import PyPDF2 def download_pdf_file(url, filename): # Get the absolute path to the desktop folder desktop_path = os.path.join(os.path.expanduser('~'), 'Desktop') file_path = os.path.join(desktop_path, filename) # Send a GET request to the PDF URL response = requests.get(url) with open(file_path, 'wb') as f: f.write(response.content) print(f"{filename} downloaded successfully at the desktop.\n") def extract_text_from_pdf(filename): with open(filename, 'rb') as f: pdf_reader = PyPDF2.PdfFileReader(f) text = "" for page_num in range(pdf_reader.numPages): page = pdf_reader.getPage(page_num) text += page.extract_text() return text # 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 and also extract text from it for link in pdf_links: pdf_url = urljoin(url, link['href']) pdf_filename = pdf_url.split('/')[-1] txt_filename = pdf_filename.replace(".pdf", ".txt") print(f"Downloading {pdf_filename}...") download_pdf_file(pdf_url, pdf_filename) print(f"Extracting text from {pdf_filename}...") pdf_text = extract_text_from_pdf(pdf_filename) # Get the absolute path to the desktop folder desktop_path = os.path.join(os.path.expanduser('~'), 'Desktop') txt_path = os.path.join(desktop_path, txt_filename) with open(txt_path, 'w', encoding='utf-8') as f: f.write(pdf_text) print(f"{txt_filename} created successfully!\n") print("All PDF files downloaded as text on the desktop.")
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.