alperiox

wrap it up

Oct 29th, 2022 (edited)
1,249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.29 KB | None | 0 0
  1. def extract_data(element):
  2.     img = element.find_element(By.TAG_NAME, "img").get_attribute("srcset")
  3.     img = parse_img_url(img)
  4.  
  5.     # A>B means the B elements where A is the parent element.
  6.     dietary_attrs = element.find_elements(By.CSS_SELECTOR, "div[class*='DietaryAttributes']>span")
  7.     # if there aren't any, then `dietary_attrs` will be None and `if` block won't work
  8.     # but if there are any dietary attributes, extract the text from them
  9.     if dietary_attrs:
  10.         dietary_attrs = [attr.text for attr in dietary_attrs]
  11.     else:
  12.         # set the variable to None if there aren't any dietary attributes found.
  13.         dietary_attrs = None
  14.  
  15.     # get the span elements where the parent is a `div` element that
  16.     # has `ItemBCardDefault` substring in the `class` attribute
  17.     price = element.find_elements(By.CSS_SELECTOR, "div[class*='ItemBCardDefault']>span")
  18.     # extract the price text if we could find the price span
  19.     if price:
  20.         price = price[0].text
  21.     else:
  22.         price = None
  23.  
  24.     name = element.find_element(By.TAG_NAME, "h2").text
  25.     size = element.find_element(By.CSS_SELECTOR, "div[class*='Size']").text
  26.  
  27.     return {
  28.         "price": price,
  29.         "name": name,
  30.         "size": size,
  31.         "attrs": dietary_attrs,
  32.         "img": img
  33.     }
Advertisement
Add Comment
Please, Sign In to add comment