Advertisement
Guest User

Untitled

a guest
Feb 25th, 2018
498
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.51 KB | None | 0 0
  1. #! /usr/bin/env python3
  2. import bs4, requests, sys, os
  3. from jinja2 import Template
  4.  
  5. searchURL = 'https://www.ebay-kleinanzeigen.de/s-10587/leuchte/k0l3346r10'
  6.  
  7. def getEbayListing(searchURL): # grab Listings
  8.  
  9.     res = requests.get(searchURL) # get html
  10.     res.raise_for_status() # check if error
  11.    
  12.     soup = bs4.BeautifulSoup(res.text, "html.parser") # parse html
  13.    
  14.     productDIVs = soup.select("div.aditem-image > div") # grab DIV
  15.  
  16.     productDict = {} # create dict to store ouput
  17.    
  18.     for product in productDIVs:
  19.  
  20.         productURL = "https://www.ebay-kleinanzeigen.de" + product.get('data-href') # grab URL
  21.         largeImage = product.get('data-imgsrc').strip("$_9.JPG") + "$_32.JPG" # grab ImageLink
  22.                
  23.         productDict[productURL] = largeImage # Populate List
  24.  
  25.     return productDict
  26.  
  27.  
  28. def createHTML(productDict):
  29.  
  30.     htmltext = Template('''<link rel="stylesheet" href="styles.css">
  31. <html>
  32. <head>
  33. <h1>Images for Search</h1>
  34. </head>
  35. <body
  36. <div class="gallery">
  37.    <div>
  38.    {% for url, image in productDict.items() %}
  39.    <a href="{{ url }}"><img src="{{ image }}" width="300" height="500"/></a>
  40.    {% endfor %}
  41.    </div>
  42. </div>
  43. </body>
  44. </html>''')
  45.  
  46.     return htmlText
  47.  
  48. sys.argv # get args
  49.  
  50. if len(sys.argv) > 1: # check args
  51.     searchURL = ' '.join(sys.argv[1:])
  52.  
  53. productDict = getEbayListing(searchURL)
  54. htmlText = createHTML(productDict)
  55.  
  56. htmlText.stream(productDict = productDict).dump('test.html')
  57. print('File successfully written to: ' + os.getcwd())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement