Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def extract_data(element):
- img = element.find_element(By.TAG_NAME, "img").get_attribute("srcset")
- img = parse_img_url(img)
- # A>B means the B elements where A is the parent element.
- dietary_attrs = element.find_elements(By.CSS_SELECTOR, "div[class*='DietaryAttributes']>span")
- # if there aren't any, then `dietary_attrs` will be None and `if` block won't work
- # but if there are any dietary attributes, extract the text from them
- if dietary_attrs:
- dietary_attrs = [attr.text for attr in dietary_attrs]
- else:
- # set the variable to None if there aren't any dietary attributes found.
- dietary_attrs = None
- # get the span elements where the parent is a `div` element that
- # has `ItemBCardDefault` substring in the `class` attribute
- price = element.find_elements(By.CSS_SELECTOR, "div[class*='ItemBCardDefault']>span")
- # extract the price text if we could find the price span
- if price:
- price = price[0].text
- else:
- price = None
- name = element.find_element(By.TAG_NAME, "h2").text
- size = element.find_element(By.CSS_SELECTOR, "div[class*='Size']").text
- return {
- "price": price,
- "name": name,
- "size": size,
- "attrs": dietary_attrs,
- "img": img
- }
Advertisement
Add Comment
Please, Sign In to add comment