Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. soup = BeautifulSoup(response.content, "lxml")
  2. product_size = []
  3. try:
  4. if "Add to Cart" in response.text:
  5. for y in soup.find_all("script", {"type": "text/javascript"}):
  6.  
  7. if "spConfig =" in y.text:
  8. regex = re.compile(r'var spConfig = new Product.Config\((.*?)\);')
  9. match = regex.search(y.text)
  10. spConfig = json.loads(match.groups()[0])
  11.  
  12. for key in spConfig['attributes']: # Should only call once
  13. for product in spConfig['attributes'][key]['options']:
  14. product_size.append(product['label_uk'])
  15. except:
  16. pass
  17.  
  18. if len(product_size) == 0:
  19. product_size = "No Stock"
  20.  
  21. try:
  22. product_price = soup.find('span', {'class': 'price'}).text
  23. except:
  24. product_price = "Null"
  25.  
  26. try:
  27. product_name = soup.find('meta', {'property': 'og:title'})["content"]
  28. except:
  29. product_name = product_link
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement