Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests, bs4, re, time
- from selenium import webdriver
- from selenium.webdriver.firefox.options import Options
- from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
- from multiprocessing import Pool, cpu_count
- from itertools import repeat
- def make_driver():
- options = Options()
- options.headless = True
- return webdriver.Firefox(options=options)
- def stripper(string):
- if string == '':
- return string
- strung = string
- if string[0] == " ":
- strung = strung[1:]
- if string[-1] == " ":
- strung = strung[:-1]
- return strung
- def get_product(serial, driver, warranty=True):
- global desired_countries
- baseUrl = "https://pcsupport.lenovo.com/us/en/basicwarrantylookup?sn="
- driver.get(baseUrl+serial)
- print(f"Retrieved webpage for serial number {serial}")
- soup = bs4.BeautifulSoup(driver.page_source, features="html.parser", multi_valued_attributes=None)
- timewaited = 1
- while soup.find_all('div', class_="l-vue-loading__wrapper loading-fullscreen l-vue-fullscreen")[0]['style'][-14:] != "display: none;":
- time.sleep(0.1)
- print(f"Waited {float(str(timewaited)[:-1]+'.'+str(timewaited)[-1])} secs", end="\r",flush=True)
- timewaited += 1
- soup = bs4.BeautifulSoup(driver.page_source, features="html.parser", multi_valued_attributes=None)
- print()
- print("Got HTML from webpage")
- if soup.find_all('p', string="No matched data found") != []:
- print("Not a valid serial number; cancelling")
- return False
- countryheadtag = soup.find_all('label', string="Country/Region")[0]
- countrytag = countryheadtag.next_sibling
- print(f"Country is {str(countrytag.string)}")
- if str(countrytag.string) in desired_countries:
- if warranty:
- warrantytag = soup.find_all('span', string=re.compile("Advanced Exchange Warranty"))
- print(f"Got warranty: {warrantytag if warrantytag else 'not valid'}")
- if warrantytag:
- print("Hit!")
- productheadtag = soup.find_all('label', string="Product")[0]
- producttag = productheadtag.next_sibling
- print(f"Product is {str(producttag.string)}")
- with open("output.csv", "a") as out:
- out.write(serial+","+str(producttag.string)+","+str(countrytag.string)+","+str(warrantytag[0].string if warrantytag else [])+"\n")
- out.flush()
- return True
- else:
- with open("output.csv", "a") as out:
- out.write(serial+","+str(producttag.string)+","+str(countrytag.string)+",[]\n")
- out.flush()
- return True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement