【问题标题】:Data scraping text without css path R没有css路径R的数据抓取文本
【发布时间】:2017-07-07 13:10:44
【问题描述】:

您好,我给您写信是因为我正在努力寻找一种方法并从网页中删除数据(“https://nabtu.org/about-nabtu/official-directory/building-trades-local-councils-overview/”)。 我这样做是为了练习,只是为了学习如何删除数据。我正在尝试删除上述网页(办公室、传真、电子邮件)的联系数据,但由于没有确定的 css 路径,我无法做到我可以使用 Selectorgadget。我使用的是 R,我使用的脚本是这样的。

library(rvest)
page_name <-read_html("page html")


page_name %>%
html_node("selector gadget node") %>%
html_text()

我删除了所有其他数据,但无法删除此联系信息。 任何帮助将不胜感激,因为我的头要炸了。在此先感谢。

【问题讨论】:

  • “您不得…… (h) 使用任何自动或手动过程从站点获取信息;” 您的部分自我教育应该是学习不违反站点使用条款/服务条款。

标签: css r web web-scraping


【解决方案1】:

我看不出问题出在哪里。每个联系人块都有一个.council-list 列表类。使用它,您可以单独提取联系信息。然后,使用一些字符串/正则表达式操作来提取确切的字段。

library(rvest)
page_name <- read_html('https://nabtu.org/about-nabtu/official-directory/building-trades-local-councils-overview/')
contact_strings = page_name %>%
  html_nodes('.council-list') %>%
  html_text()

# Filter out strings that don't contain contact information
contact_strings = grep(x = contact_strings, 'Email|Fax|office', ignore.case = T, value = T)

# Extract infomration 
library(stringr)
library(magrittr)
office = str_extract(contact_strings, 'Office:[^[:alpha:]]*')
fax = str_extract(contact_strings, 'Fax:[^[:alpha:]]*')
email = str_extract(contact_strings, 'Email: [^ ]*')

【讨论】:

  • 非常感谢。我是这个领域的新手,正在努力适应它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-26
  • 1970-01-01
  • 1970-01-01
  • 2018-10-22
  • 1970-01-01
相关资源
最近更新 更多