【问题标题】:Python BeautifulSoup Use Text out of tags and store as variablePython BeautifulSoup 使用标签中的文本并存储为变量
【发布时间】:2021-07-17 03:22:25
【问题描述】:

我目前正在尝试解析来自特定网页的文本,到目前为止效果很好。我只是在努力“让”文本进一步使用它。

到目前为止,我的代码如下所示:

basename (URL which will be scraped in general)

request_two = requests.get("https://www.billiger.de/shops?shopsearch=" + basename)

def find_tags_from_class(html):

soup = BeautifulSoup(html.content, "html.parser")
div = soup.find("div", class_="svg-rating-stars large blue")
print(div)

这会打印出<div class="svg-rating-stars large blue" style="--rating: 100.0"></div><div class="svg-rating-stars large blue" style="--rating: 98.3"></div>(取决于用户要检查的商店(基本名称))

由于这可能会有所不同,我想检查在“svg-rating-stars large blue”类中解析的评分是否大于或等于 80。有可能这样做吗?我对 python 和 webscraping 还很陌生,现在找不到任何解决方案。

【问题讨论】:

    标签: python html css web-scraping


    【解决方案1】:

    您可以使用get 访问 div 属性。然后您必须将字符串转换为浮点数,以便确定它是否为 GTE 80。Rating 是布尔值,如果 >= 80 则为 True,否则为 False。

    rating = False
    if div.get('style'):
        try:
            rating = float(div.get('style').split()[-1]) >= 80
        except:
            pass
    

    【讨论】:

      猜你喜欢
      • 2013-11-20
      • 1970-01-01
      • 2020-01-15
      • 1970-01-01
      • 2019-01-17
      • 2020-10-21
      • 1970-01-01
      • 2023-03-03
      • 1970-01-01
      相关资源
      最近更新 更多