-3

I am trying to get a specific string in a specific position. The string I have is a Json inside html page I tried to locate it using HTML tags But I could not The site I want to scarap the string I want to get is: "isOnlyFewLeft":false,"isOutOfStock":false,"isRopisEligibleSku":true,"isSephoraExclusive":false,"

I tried the following pattern: '''"isOutOfStock": (.*?), "isRopisEligibleSku"''' But it did not work. My code: ```

def Filter(self):
    try:
        print(self.sourcepage)
        match = re.search('''"isOutOfStock": (.*?), "isRopisEligibleSku"''', self.sourcepage)
        print(match)
        if match:
            print(match.group())
    except BaseException as e:
        print(e)```  
Hassan Ibraheem
  • 118
  • 1
  • 7

1 Answers1

-1

Try to get required values by parsing script HTML-element as JSON:

import requests
from bs4 import BeautifulSoup
import json

url = 'https://www.sephora.com/product/laura-mercier-mini-tinted-moisturizer-broad-spectrum-spf-20-oil-free-P466665?icid2=just%20dropped:p466665:product'
response = requests.get(url)
soup = BeautifulSoup(r.content,'lxml')
script = soup.select('#linkStore')[0].text
content = json.loads(script)

product_info = content['page']['product']['ancillarySkus'][0]
print(product_info['isOnlyFewLeft'],
      product_info['isOutOfStock'],
      product_info['isSephoraExclusive'],
      product_info['isRopisEligibleSku'])

Output:

(False, False, False, True)
JaSON
  • 4,843
  • 2
  • 8
  • 15