簡體   English   中英

使用 python 刮取 web 頁面

[英]Scrape web pages using python

我有以下 web 頁面

</div><a href="https://www.emag.ro/laptop-lenovo-thinkbook-15-iil-cu-procesor-intel-core-i7-1065g7-pana-la-3-90-ghz-15-6-full-hd-16gb-512gb-ssd-intel-iris-plus-graphics-free-dos-mineral-grey-20sm003jrm/pd/DKBK1TMBM/#reviews-section" rel="nofollow" class="star-rating-container js-product-url" data-zone="reviews"><div class="star-rating star-rating-read rated-4.02  star-rating-sm  ">
        <div class="star-rating-inner " style="width: 100%"></div>
    </div><div class="star-rating-text ">

我想從這個產品中提取評級。 對於此產品,評級在此處定義。

<div class="star-rating star-rating-read rated-4.02  star-rating-sm  ">

而且我無法提取4.02。

我的代碼如下所示:

rating = container.find_all(class_="star-rating star-rating-read rated")[0].text

我知道上面的代碼不行,我可以提取產品的價格和名稱,但我無法提取評級:(

這是您可以嘗試的解決方案,

import re

# regex extract the decimal digits from string
extract_ = re.compile(r"\d+.\d+") 

for div in container.find_all("div", attrs={"class": 'star-rating'}):
    for attr in div.attrs['class']:
        ratings_ = extract_.search(attr)

        if ratings_:
            print(ratings_.group())  # 4.02

嘗試這樣的事情:

rating = str(container.find_all(class_="star-rating")[0])
rindex = rating.index("rated")
print(rating[rindex+6:rindex+10])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM