在python中,如何在两个字符串之间获取特定字符串

2024-10-02 02:30:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试在特定位置获取特定字符串。我拥有的字符串是html页面中的Json,我试图使用html标记定位它,但我无法The site I want to scarap 我想要得到的字符串是:"isOnlyFewLeft":false,"isOutOfStock":false,"isRopisEligibleSku":true,"isSephoraExclusive":false,"

我尝试了以下模式:'''"isOutOfStock": (.*?), "isRopisEligibleSku"''',但它不起作用。 我的代码:```

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)```  

Tags: the字符串标记定位selfjsonfalsehtml
1条回答
网友
1楼 · 发布于 2024-10-02 02:30:34

尝试通过将scriptHTML元素解析为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'])

输出:

(False, False, False, True)

相关问题 更多 >

    热门问题