Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用R从网站源代码/html中获取信息?_Html_R_Xpath_Web Scraping - Fatal编程技术网

如何使用R从网站源代码/html中获取信息?

如何使用R从网站源代码/html中获取信息?,html,r,xpath,web-scraping,Html,R,Xpath,Web Scraping,我对网络垃圾相当陌生,我正在尝试构建一个scraper,它使用R访问网站源代码/html中的信息 具体地说,我希望能够确定一个(多个)网站是否有一个带有特定文本的id:“google_ads_iframe”。id总是比这个长,所以我想我必须使用通配符 我尝试了几种选择(见下文),但到目前为止没有任何效果 第一种方法: doc <- htmlTreeParse("http://www.funda.nl/") data <- xpathSApply(doc, "//div[conta

我对网络垃圾相当陌生,我正在尝试构建一个scraper,它使用R访问网站源代码/html中的信息

具体地说,我希望能够确定一个(多个)网站是否有一个带有特定文本的id:“google_ads_iframe”。id总是比这个长,所以我想我必须使用通配符

我尝试了几种选择(见下文),但到目前为止没有任何效果

第一种方法:

doc <- htmlTreeParse("http://www.funda.nl/") 

data <- xpathSApply(doc, "//div[contains(@id, 'google_ads_iframe')]", xmlValue, trim = TRUE) 
第二种方法:

scrapestuff <- scrape(url = "http://www.funda.nl/", parse = T, headers = T)

x <- xpathSApply(scrapestuff[[1]],"//div[contains(@class, 'google_ads_iframe')]",xmlValue)

scrapestuff我的广告拦截器可能会阻止我看到谷歌广告iframe,但你不必浪费额外的R函数来测试是否存在某些东西。让
libxml2
中的优化C函数(它是
rvest
xml2
包的基础)为您完成这项工作,只需使用
boolean()来包装XPath即可:

不过,这是一个呈现的页面,我怀疑您仍然需要使用RSelenium来抓取页面。以下是如何做到这一点(如果您使用的是合理的操作系统,并且安装了phantomjs,否则请与Firefox一起使用):

库(RSelenium)
RSelenium::startServer()

phantom_js函数来自哪些包?计算文本中出现次数的简单方法是使用
sum(stringr::str_count(scrapeTuff,“谷歌广告iframe”)
如果scrapeTuff可以是字符。检查一下。实际上,对于HTML/XML,计算HTML/XML中出现次数的简单方法是使用例如-
XML\u find\u first(pg,“count(.//div[contains(@class,'featured')))”)
谢谢,不幸的是,它不太管用。你的例子很好。当将此用作以下示例时,它不会返回false,即使我知道它应该是true:xml\u find\u lgl(pg,“boolean(.//div[contains(@id,google\u ads\u iframe'))”),因为div不包含该id,它包含一个带有该id的iframe,这就是我使用该示例的原因。在第二步中,我想看看是否有某个源用于该广告,并且我使用的是src。我尝试了以下方法,但没有成功:xml_find_lgl(pg,“boolean(.//div[iframe[contains(@src,'googleadservices.com')]))通常不是
img
脚本
标记中的那些吗?我知道
iframe
s也可以有
src
属性,但对于谷歌广告,它们通常是
javascript
,并且不使用该域。我认为设置不同,但一般来说你可能是对的。我尝试了脚本而不是iframe,但也没有产生任何结果。我也试着把div删掉。
scrapestuff <- scrape(url = "http://www.funda.nl/", parse = T, headers = T)

x <- xpathSApply(scrapestuff[[1]],"//div[contains(@class, 'google_ads_iframe')]",xmlValue)
scrapestuff <- read_html("http://www.funda.nl/")
hh <- htmlParse(scrapestuff, asText=T)
x <- xpathSApply(hh,"//div[contains(@id, 'google_ads_iframe')]",xmlValue)
library(xml2)

pg <- read_html("http://www.funda.nl/")

xml_find_lgl(pg, "boolean(.//div[contains(@class, 'featured')])")
## [1] TRUE

xml_find_lgl(pg, "boolean(.//div[contains(@class, 'futured')])")
## [1] FALSE
pg <- read_html("http://codepen.io/anon/pen/Jtizx.html")

xml_find_lgl(pg, "boolean(.//div[iframe[contains(@id, 'google_ads_iframe')]])")
## [1] TRUE

xml_find_first(pg, "count(.//div[iframe[contains(@id, 'google_ads_iframe')]])")
## [1] 3
library(RSelenium)
RSelenium::startServer()
phantom_js <- phantom(pjs_cmd='/usr/local/bin/phantomjs', extras=c("--ssl-protocol=any"))
capabilities <- list(phantomjs.page.settings.userAgent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.70 Safari/537.3")
remDr <- remoteDriver(browserName = "phantomjs", extraCapabilities=capabilities)
remDr$open()

remDr$navigate(URL)
raw_html <- remDr$getPageSource()[[1]]

pg <- read_html()
...

# eventually (when done)
phantom_js$stop()
<div id="div-gpt-ad-1379506098645-3" style="width:720px;margin-left:auto;margin-right:auto;display:none;">
  <script type="text/javascript">
  googletag.cmd.push(function() { googletag.display('div-gpt-ad-1379506098645-3'); });
  </script>
  <iframe id="google_ads_iframe_/16833175/SmallPS_0" name="google_ads_iframe_/16833175/SmallPS_0" width="723" height="170" scrolling="no" marginwidth="0" marginheight="0" frameborder="0" src="javascript:&quot;<html><body style='background:transparent'></body></html>&quot;" style="border: 0px; vertical-align: bottom;"></iframe></div>