【问题标题】:python - selenium scraping rotten tomatoes for audience scorepython - 硒刮烂番茄获取观众评分
【发布时间】:2022-06-10 19:46:27
【问题描述】:

我正在尝试从烂番茄中获取观众分数。我能够获得评论,但不确定如何使用 selenium 获得“audiencescore”

来源:

<score-board
audiencestate="upright"
audiencescore="96"
class="scoreboard"
rating="R"
skeleton="panel"
tomatometerstate="certified-fresh"
tomatometerscore="92"
data-qa="score-panel"
                >
<h1 slot="title" class="scoreboard__title" data-qa="score-panel-movie-title">Pulp Fiction</h1>
<p slot="info" class="scoreboard__info">1994, Crime/Drama, 2h 33m</p>
<a slot="critics-count" href="/m/pulp_fiction/reviews?intcmp=rt-scorecard_tomatometer-reviews" class="scoreboard__link scoreboard__link--tomatometer" data-qa="tomatometer-review-count">110 Reviews</a>
<a slot="audience-count" href="/m/pulp_fiction/reviews?type=user&amp;intcmp=rt-scorecard_audience-score-reviews" class="scoreboard__link scoreboard__link--audience" data-qa="audience-rating-count">250,000+ Ratings</a>
<div slot="sponsorship" id="tomatometer_sponsorship_ad"></div>
                </score-board>

代码:

from selenium import webdriver

driver = webdriver.Firefox()
url = 'https://www.rottentomatoes.com/m/pulp_fiction'
driver.get(url)

print(driver.find_element_by_css_selector('a[slot=audience-count]').text)

【问题讨论】:

    标签: python selenium


    【解决方案1】:

    audiencescore 的属性值不是任何文本节点值,我们无法调用.text 方法来获取该值。因此,您必须在选择正确的定位器后致电get_attribute()。以下表达式有效。

    print(driver.find_element(By.CSS_SELECTOR,'#topSection score-board').get_attribute('audiencescore'))
    

    #导入

    from selenium.webdriver.common.by import By
    

    【讨论】:

      【解决方案2】:

      试试这个:

      1- 获取元素记分牌

      2- 从元素中获取 Audiencescore 属性

      audiencescore = driver.find_element_by_css_selector('score-board').get_attribute('audiencescore')
      

      【讨论】:

        【解决方案3】:

        你已经够近了。要提取 audiencescore 属性的值,即文本 96,理想情况下,您需要为 visibility_of_element_located() 诱导 WebDriverWait,然后您可以使用以下任一locator strategies

        • 使用CSS_SELECTOR

          driver.get("https://www.rottentomatoes.com/m/pulp_fiction")
          print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "score-board.scoreboard"))).get_attribute("audiencescore"))
          
        • 使用XPATH

          driver.get("https://www.rottentomatoes.com/m/pulp_fiction")
          print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//score-board[@class='scoreboard']"))).get_attribute("audiencescore"))
          
        • 注意:您必须添加以下导入:

          from selenium.webdriver.support.ui import WebDriverWait
          from selenium.webdriver.common.by import By
          from selenium.webdriver.support import expected_conditions as EC
          
        • 控制台输出:

          96
          

        您可以在How to retrieve the text of a WebElement using Selenium - Python找到相关讨论

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-12-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多