Hi I have following code which i want to download the pdf from a website "https://www.journal-officiel.gouv.fr/balo/recherche/resultats?parutionDateStart=2021-05-17&parutionDateEnd=2021-05-17&_token=0oP3_cJ2xZ10SbEEGoNdP6vUpAIv5nBkrTZptI0Nzd8"
Here is the script that i am using to download the files but none of pdf are getting downloaded .
Though it is not giving any error it is creating a blank folder every time
from selenium import webdriver
from selenium.webdriver.support.ui import Select
driver = webdriver.Chrome(executable_path='C:\\Users\\u6080267\\Documents\\chromedriver.exe')
driver.get("https://www.journal-officiel.gouv.fr/balo/recherche/")
link = driver.find_element_by_xpath("//a[contains(@href,'token')]")
link.click()
url1 = driver.current_url
import urllib.request
from bs4 import BeautifulSoup
import os
import urllib
from datetime import datetime
import requests
HEADERS = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36'}
def get_urls(url):
url = url1
#url = https://www.journal-officiel.gouv.fr/balo/recherche/resultats?parutionDateStart=2021-05-03&parutionDateEnd=2021-05-03&_token=48BMi0HUW0CZJVdbccoO_wX9IzRJfglO8Uq-K0lfMNg
req = urllib.request.Request(url, None, HEADERS)
opener = urllib.request.build_opener()
content = opener.open(req).read()
soup = BeautifulSoup(content, "html.parser")
soup.prettify()
urls = {}
for anchor in soup.findAll('a', href=True): #Going inside links
if "/balo/document" in anchor.get('href'):
name = anchor.get('href')[(anchor.get('href').rindex("=")+1):]
url = "https://www.journal-officiel.gouv.fr/" + anchor.get('href')
if name not in urls:
urls[name]=url
return urls
def download(urls, path):
os.chdir(path)
for name, url in urls.items():
try:
res = requests.get(url, allow_redirects=True)
# programmatic access requires a form to be submitted. On agreeing the consent, the pdfurl can be used
soup = BeautifulSoup(res.content, "html.parser")
soup.prettify()
for pdfurl in soup.findAll(attrs={"name": "pdfURL"}):
downloadurl = "https://www.journal-officiel.gouv.fr/" + pdfurl.get('value')
res = requests.get(downloadurl)
open(name + ".pdf", 'wb').write(res.content)
print ("Downloaded", name + ".pdf")
except Exception as e:
print ("Failed to download", name, ", because of", e)
def main():
pathToStoreFiles = os.getcwd() + "\\" + datetime.today().strftime('%Y-%m-%d')
os.makedirs(pathToStoreFiles)
urls = get_urls('')
download(urls, pathToStoreFiles)
if __name__ == "__main__":
main()