【问题标题】:Cannot find the correct CSS selector for webscraping无法为网页抓取找到正确的 CSS 选择器
【发布时间】:2021-09-25 21:19:26
【问题描述】:

我正在尝试从 IMDb 中获取一些电视收视率,但找不到正确的 CSS 选择器。我尝试了几十种替代方法,并尝试使用 CSS 选择器小工具,但 R 正在返回 {xml_nodeset (0)} 值。

这是我的代码:

require(rvest)

read_html('https://www.imdb.com/title/tt0562992/?ref_=ttep_ep1') %>% 
  html_nodes('a.ipc-button ipc-button--single-padding ipc-button--center-align-content ipc-button--default-height ipc-button--core-baseAlt ipc-button--theme-baseAlt ipc-button--on-textPrimary ipc-text-button RatingBarButtonBase__Button-sc-15v8ssr-2 jjcqHZ')

我想要做的是提取嵌入在页面左上角评级中的 href。请参阅下面的图片,其中我突出显示了我希望在开发人员视图 CSS 中提取的 href。

有人可以帮我找出正确的选择器吗?

【问题讨论】:

  • 尝试 'div[class^=hero-rating-bar] > a[class^=ipc-button]' 如果你想走 CSS 路线,这使用部分选择,所以 ^ 表示类开始。希望这会有所帮助!

标签: html r web-scraping css-selectors rvest


【解决方案1】:

我认为这个问题是因为某些类末尾的 ID,如“jjcqHZ”、“15v8ssr”。 许多前端框架每次刷新都会更改这些 ID。 尝试选择没有这些类的元素。

【讨论】:

  • 嗯,很有趣。这将返回与我之前看到的相同的 xml_nodeset(0) 值。我运行的确切代码:read_html('https://www.imdb.com/title/tt0562992/?ref_=ttep_ep1') %>% html_nodes('a.ipc-button ipc-button--single-padding ipc-button--center-align-content ipc-button--default-height ipc-button--core-baseAlt ipc-button--theme-baseAlt ipc-button--on-textPrimary ipc-text-button RatingBarButtonBase__Button-sc')
【解决方案2】:

检查元素,然后右键复制>复制选择器

【讨论】:

  • 这是一个很好的提示,但它并没有解决我的问题。我运行的确切代码:html_nodes('#__next > main > div > section.ipc-page-background.ipc-page-background--base.TitlePage__StyledPageBackground-wzlr49-0.dDUGgO > section > div:nth-child(4) > section > section > div.TitleBlock__Container-sc-1nlhx7j-0.hglRHk > div.RatingBar__RatingContainer-sc-85l9wd-0.hNqCJh.TitleBlock__HideableRatingBar-sc-1nlhx7j-4.bhTVMj > div > div:nth-child(1) > a > div > div > div.AggregateRatingButton__ContentWrap-sc-1ll29m0-0.hmJkIS > div.AggregateRatingButton__TotalRatingAmount-sc-1ll29m0-3.jkCVKJ')
【解决方案3】:

我实际上会使用 XPath,因为类是非常动态的,在这种情况下不是很可靠。

<div>foobar</div>
function getElementByXpath(path) {
  return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
}

console.log(getElementByXpath("//html[1]/body[1]/div[1]") );

【讨论】:

  • 这看起来很有希望,而且是一个很好的提示。您在上面共享的代码虽然超出了我的范围。这就是我运行的内容:read_html('https://www.imdb.com/title/tt0562992/?ref_=ttep_ep1') %&gt;% html_nodes(xpath = '//*[@id="__next"]/main/div/section[1]/section/div[3]/section/section/div[1]/div[2]/div/div[1]/a/div/div/div[2]/div[1]') 但不幸的是,当我尝试通过将上述内容管道到html_attr('href') 来提取href 时,它返回一个NA
  • 我对 R 不熟悉,但如果它与 python 类似,您可以将 html 解析为 beautifulsoup 之类的包,然后以这种方式提取 href?我在这里避免使用正则表达式,因为这不是完全证据。
  • 好吧!我正在使用rvest,据我所知,这是 R 的等价物
【解决方案4】:

你可以使用 xpath -

library(rvest)
url <- 'https://www.imdb.com/title/tt0562992/?ref_=ttep_ep1'

url %>% 
  read_html() %>%
  html_element(xpath = '//*[@aria-label="View User Ratings"]') %>%
  #For older version of rvest use `html_node`
  #html_node(xpath = '//*[@aria-label="View User Ratings"]') %>%
  html_attr('href') %>%
  paste0('https://www.imdb.com', .)

#[1] "https://www.imdb.com/title/tt0562992/ratings/?ref_=tt_ov_rt"

【讨论】:

    【解决方案5】:

    不妨完成这组建议。


    您无需担心动态类。使用多值类中的一个稳定类作为父类,然后子类组合得到子类a标签:

    library(rvest)
    library(magrittr)
    
    url <- "https://www.imdb.com/title/tt0562992/?ref_=ttep_ep1"  
    link <- read_html(url) %>%
      html_element(".rating-bar__base-button > a") %>%
      html_attr("href") %>%
      url_absolute(url)
    

    或者,由于 IMDb 对这些事情有一致的方法,请避免发出请求,而只需对 url 的查询字符串部分进行替换。您可以将其封装到评级函数中。

    url <- "https://www.imdb.com/title/tt0562992/?ref_=ttep_ep1"
    link <- gsub("(\\?ref_=.*)", "ratings/?ref_=tt_ov_rt", url)
    

    【讨论】:

    • 这是有教育意义的。谢谢。我希望将 href 嵌入评级中,而不是实际评级。所以我接受了 Ronak 的回答
    • 这很棒,而且绝对更简单。我怎么能自己找不到这个 CSS 呢?我花了几个小时寻找。是的,在发布我的问题几个小时后,我意识到我可以更改 URL 的结尾(我正在做很多这样的事情,所以避免请求是一个真正的优势)。我最终得到:all_links %&gt;% mutate(ratings_link = str_sub(.$link, 1, str_locate(.$link, "\\?")[,1]-2), ratings_link = paste0('https://www.imdb.com', ratings_link, '/ratings/?ref_=tt_ov_rt'))
    • 依靠工具为您提供 css 选择器,或右键单击检查,是很好的起点。没有什么可以替代实践。工具通常会给你一些过于复杂的东西。学习技巧,如如何在多值类中发现一个稳定的类、如何使用部分属性值以及how to construct your own relationships 需要练习。有关 css 选择器资源的一些链接,请参阅我的个人资料页面。
    • 感谢 QHarr。这些看起来像是一些很好的资源——我一定会深入研究它们
    猜你喜欢
    • 2021-10-08
    • 1970-01-01
    • 2021-05-29
    • 2021-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-16
    相关资源
    最近更新 更多