【问题标题】:Web scraping using rvest: css selector to get "more text"使用 rvest 进行网页抓取:css 选择器以获取“更多文本”
【发布时间】:2021-06-29 07:24:46
【问题描述】:

我正在查看一个从网站抓取文本数据并努力从特定部分获取所有文本的示例,特别是该文本框有一个名为“阅读更多”的字段。

我尝试了不同的 css 选择器(使用 Selector Gadget 识别)但没有成功,并且捕获的文本不是所有可用的文本。

关于如何获取完整文本字段的任何想法?

谢谢!

library(rvest)

link = "https://www.property24.com/for-sale/camps-bay/cape-town/western-cape/11014/109734849"

html_link = read_html(link)

# Method 1
text1 = html_link %>%
  html_nodes(css = ".js_readMoreText") %>%
  html_text()
text1

# Method 2
text2 = html_link %>%
  html_nodes(css = ".js_readMore") %>%
  html_text()
text2

# Method 3
text3 = html_link %>%
  html_nodes(css = ".expanded , .js_readMoreText") %>%
  html_text()
text3

【问题讨论】:

    标签: r web-scraping css-selectors rvest


    【解决方案1】:

    该内容存储在元标记的内容属性中。您可以选择如下:

    library(rvest)
    
    link <- "https://www.property24.com/for-sale/camps-bay/cape-town/western-cape/11014/109734849"
    html_link <- read_html(link)
    
    description <- html_link %>%
      html_node('[property="og:description"]') %>%
      html_attr('content')
    

    【讨论】:

    • 能否分享您如何确定内容位于元标记中?我在网页上使用了 Selector Gadget,它有助于理解你是如何拉取它的。谢谢!
    猜你喜欢
    • 2019-10-11
    • 2018-02-15
    • 2018-10-27
    • 2017-03-28
    • 2019-02-22
    • 2015-09-06
    相关资源
    最近更新 更多