Advertisement
Guest User

lenove!

a guest
Mar 21st, 2020
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.69 KB | None | 0 0
  1. import requests, bs4, re, time
  2. from selenium import webdriver
  3. from selenium.webdriver.firefox.options import Options
  4. from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
  5. from multiprocessing import Pool, cpu_count
  6. from itertools import repeat
  7.  
  8.  
  9. def make_driver():
  10.     options = Options()
  11.     options.headless = True
  12.     return webdriver.Firefox(options=options)
  13.  
  14.  
  15. def stripper(string):
  16.     if string == '':
  17.         return string
  18.     strung = string
  19.     if string[0] == " ":
  20.         strung = strung[1:]
  21.     if string[-1] == " ":
  22.         strung = strung[:-1]
  23.     return strung
  24.  
  25. def get_product(serial, driver, warranty=True):
  26.     global desired_countries
  27.     baseUrl = "https://pcsupport.lenovo.com/us/en/basicwarrantylookup?sn="
  28.     driver.get(baseUrl+serial)
  29.     print(f"Retrieved webpage for serial number {serial}")
  30.     soup = bs4.BeautifulSoup(driver.page_source, features="html.parser", multi_valued_attributes=None)
  31.     timewaited = 1
  32.     while soup.find_all('div', class_="l-vue-loading__wrapper loading-fullscreen l-vue-fullscreen")[0]['style'][-14:] != "display: none;":
  33.         time.sleep(0.1)
  34.         print(f"Waited {float(str(timewaited)[:-1]+'.'+str(timewaited)[-1])} secs", end="\r",flush=True)
  35.         timewaited += 1
  36.         soup = bs4.BeautifulSoup(driver.page_source, features="html.parser", multi_valued_attributes=None)
  37.     print()
  38.     print("Got HTML from webpage")
  39.  
  40.     if soup.find_all('p', string="No matched data found") != []:
  41.         print("Not a valid serial number; cancelling")
  42.         return False
  43.  
  44.     countryheadtag = soup.find_all('label', string="Country/Region")[0]
  45.     countrytag = countryheadtag.next_sibling
  46.     print(f"Country is {str(countrytag.string)}")
  47.     if str(countrytag.string) in desired_countries:
  48.         if warranty:
  49.             warrantytag = soup.find_all('span', string=re.compile("Advanced Exchange Warranty"))
  50.             print(f"Got warranty: {warrantytag if warrantytag else 'not valid'}")
  51.             if warrantytag:
  52.                 print("Hit!")
  53.                 productheadtag = soup.find_all('label', string="Product")[0]
  54.                 producttag = productheadtag.next_sibling
  55.                 print(f"Product is {str(producttag.string)}")
  56.             with open("output.csv", "a") as out:
  57.                 out.write(serial+","+str(producttag.string)+","+str(countrytag.string)+","+str(warrantytag[0].string if warrantytag else [])+"\n")
  58.                 out.flush()
  59.                 return True
  60.         else:
  61.             with open("output.csv", "a") as out:
  62.                 out.write(serial+","+str(producttag.string)+","+str(countrytag.string)+",[]\n")
  63.                 out.flush()
  64.                 return True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement