Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.72 KB | None | 0 0
  1. from selenium import webdriver
  2. import requests, sys, webbrowser, bs4, string, time, os
  3.  
  4. driver = webdriver.Chrome(executable_path=r'C:\Users\mbarn\Desktop\PythonScripts\chromedriver.exe')
  5. driver.get('https://gelbooru.com/index.php?page=post&s=view&id=4880645&tags=23_(real_xxiii)')
  6.  
  7. url = 'https://gelbooru.com/index.php?page=post&s=view&id=4880645&tags=23_(real_xxiii)'
  8. os.makedirs('C:/Users/mbarn/Desktop/23', exist_ok = True)
  9.  
  10. while not url.endswith('#'):
  11.     #Download the page
  12.     print('Downloading page %s. . .',  (url))
  13.     res = requests.get(url)
  14.     res.raise_for_status()
  15.  
  16.     if res.status_code == 200:
  17.         print('Success!')
  18.     elif res.status_code == 404:
  19.         printf('Not found')
  20.  
  21.  
  22.     soup = bs4.BeautifulSoup(res.text)
  23.     imageElem = driver.find_element_by_xpath("//meta[@property='og:image']")
  24.  
  25.     if imageElem == []:
  26.         print('Could not find image')
  27.     else:
  28.         print(str(imageElem[0]))
  29.         imageUrl =  imageElem[0].get('content')
  30.         print('Downloading image %s. . . ', (imageUrl))
  31.         res = requests.get(imageUrl)
  32.         res.raise_for_status()
  33.  
  34.         if res.status_code == 200:
  35.             print('Successfl download')
  36.         elif res.status_code == 404:
  37.             printf('Unsuccessful download')
  38.  
  39.         #Save image to folder 23.
  40.     imageFile = open(os.path.join("C:/Users/mbarn/Desktop/23", os.path.basename(imageUrl)),'wb' )
  41.  
  42.     for chunk in res.iter_content(5000000):
  43.         imageFile.write(chunk)
  44.     imageFile.close()
  45.  
  46.         #Click the next button.
  47.     nextButton = driver.find_elements_by_link_text('Next')[0]
  48.     nextButton.click()
  49.     nextUrl = soup.select("img#image")
  50.     imageUrl = nextUrl[0].get('src')
  51. print('All images downloaded.')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement