- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想抓取大量网站。为此,我首先阅读网站的 html 脚本并将它们存储为 xml_nodesets。由于我只需要网站的内容,我最后从 xml_nodesets 中提取每个网站的内容。为此,我编写了以下代码:
# required packages
library(purrr)
library(dplyr)
library(xml2)
library(rvest)
# urls of the example sources
test_files <- c("https://en.wikipedia.org/wiki/Web_scraping", "https://en.wikipedia.org/wiki/Data_scraping")
# reading in the html sources, storing them as xml_nodesets
test <- test_files %>%
map(., ~ xml2::read_html(.x, encoding = "UTF-8"))
# extracting selected nodes (contents)
test_tbl <- test %>%
map(., ~tibble(
# scrape contents
test_html = rvest::html_nodes(.x, xpath = '//*[(@id = "toc")]')
))
不幸的是,这会产生以下错误:
Error: All columns in a tibble must be vectors.
x Column `test_html` is a `xml_nodeset` object.
我想我明白这个错误的实质,但我找不到解决方法。这也有点奇怪,因为我在一月份能够顺利运行这段代码,突然它不再起作用了。我怀疑包更新是原因,但安装旧版本的 xml2、rvest 或 tibble 也无济于事。此外,仅抓取一个网站也不会产生任何错误:
test <- read_html("https://en.wikipedia.org/wiki/Web_scraping", encoding = "UTF-8") %>%
rvest::html_nodes(xpath = '//*[(@id = "toc")]')
你对如何解决这个问题有什么建议吗?非常感谢!
%>% html_text
从 ...
test_tbl <- test %>%
map(., ~tibble(
# scrape contents
test_html = rvest::html_nodes(.x, xpath = '//*[(@id = "toc")]')
))
...因为这不会产生此错误。不过,编辑过的代码确实如此。
最佳答案
您需要将对象存储在列表中。
test %>%
purrr::map(~tibble(
# scrape contents
test_html = list(rvest::html_nodes(.x, xpath = '//*[(@id = "toc")]'))
))
#[[1]]
# A tibble: 1 x 1
# test_html
# <list>
#1 <xml_ndst>
#[[2]]
# A tibble: 1 x 1
# test_html
# <list>
#1 <xml_ndst>
关于r - 如何使用 R 中的 purrr 映射函数将 xml-nodesets(使用 rvest 创建)放入小标题中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67419847/
我发现了一个 xsl:key 似乎不起作用的情况。 我正在将 XSLT 1 与 Xalan(已编译)一起使用,这就是正在发生的事情: 1.- 这有效:名为 的 key 测试 1 工作正常:
我正在尝试在 Java 中创建 XPath 表达式(8,默认 XPath 实现)。我正在执行以下操作: Object res = xpath.evaluate("(//*[local-name()='
我正在转换一个 XML 文档。有一个属性@prettydate这是一个类似于“Friday, May 7, 2010”的字符串。我想拆分该字符串并添加指向月份和年份的链接。我正在使用 exslt:st
我一直在阅读关于在文档中选择唯一节点(使用 Muenchian 方法)的不同问题,但在我的情况下我不能使用键(或者我不知道如何使用键),因为我正在处理节点集并且不在文件上。 并且不能在节点集上设置键。
我解析一个非常大的 xml 文件(来自 jpylyzer,一个 jp2 属性提取器)。此 xml 包含许多 JP2 图像的属性,每个图像都具有相同的元素,例如: //results/jpylyzer/
我在 Nokogiri 中使用 XPath 解析从 Google 联系人 API 返回的 XML。 def getcontact doc = Nokogiri::XML.parse(open(ur
我正在使用 Nokogiri 的 xpath 方法寻找我的 input 元素。它返回类 Nokogiri::XML::NodeSet 的对象: [#, #, #, #, #]> 有没有比使用 to_s
我正在尝试在 Memcache 中缓存 Weatherman 的响应 ( https://github.com/dlt/yahoo_weatherman ) 以避免多次获取天气,我正在这样做: w
我拉了一个Nokogiri::XML::NodeSet来自页面,结果如下: 我无法弄清楚如何转换它(对人类来说很明显)标记到 Mechanize/Nokogiri 解析的对象中,这样我就
我正在使用 Crystal,并试图检索 XML 文档中节点的 ID: 我正在使用以下代码获取 ID require "xml" file = File.read("path/to/doc.xml")
我正在使用 R 中的 xml2 包解析一些非常大的 xml 文件。read_xml() 成功加载了大文件,但是当我尝试使用 xml_find_all() 时,我收到“错误:内存分配失败:增长的节点集命
我正在使用 R 中的 xml2 包解析一些非常大的 xml 文件。read_xml() 成功加载了大文件,但是当我尝试使用 xml_find_all() 时,我收到“错误:内存分配失败:增长的节点集命
我试图为 xml 文件中的特定元素设置一个属性,并且我成功地使用了 doc.css('Object').attr("Id").value = timestamp 这很好,直到“对象”不存在导致程序异常
我想按标题值对 Nokigiri 节点集进行排序。尝试了以下代码,但排序不起作用:/有什么建议吗? doc.xpath("//item").sort{|x,y| x.xpath('foo:attr[@
我想从 Nokogiri NodeSet 中获取未转义的内部 html。有谁知道如何做到这一点? 最佳答案 有什么不妥吗? nodeset.inner_html 关于ruby - 如何获取 ruby
我有一个 XmlNodeList,我需要将其设置为一种格式,然后我可以通过从 C# 扩展方法调用它来在 XSLT 样式表中重复使用它。 有人可以帮忙吗?我读到它可能与使用 XPathNavigator
我开始在 w3cschool 学习 xpath。我正在尝试评估游戏中的玩家数量。 (因此计算屏幕名称和 text() 元素/属性) 这是我的示例 xml: My Game
我有一个像这样的颜色元素序列: 还有一个单词序列: 1px Blue Solid 找到 Colors/Color/@name 属性与 Wo
当试图模拟 Faraday::Response对象,我试过: mock(Faraday::Response, :env => {:body => "...some xml..."}) ...但是当我调
我的代码在 Jetty 中工作正常,但一旦在 WebSphere 中部署,我就会收到 java.lang.VerifyError 异常,根本没有任何额外信息(甚至没有消息)。 代码: Class c
我是一名优秀的程序员,十分优秀!