下载页面不起作用.这是我得到的错误:
Error in which(value == defs) :
argument "code" is missing, with no default
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
require(RCurl)
require(XML)
ok <- "http://www.okcupid.com/match?filter1=0,34&filter2=2,22,40&filter3=3,5&filter4=5,3600&filter5=9,486&filter6=1,1&locid=4265540&lquery=San%20Francisco,%20California&timekey=1&matchOrderBy=MATCH&custom_search=0&fromWhoOnline=0&mygender=m&update_prefs=1&sort_type=0&sa=1&using_saved_search=&count=50"
okc <- getURL(ok, encoding="UTF-8") #Download the page
okcHTML <- htmlParse(okc, asText = TRUE, encoding = "utf-8")
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用美国劳工统计局的所有美国县失业数据创建一个data.frame:
http://www.bls.gov/lau/#cntyaa
从1990年到2013年,这些数据每年有一个结果.
我原本计划使用BLS API,但看起来他们认为每个县都是一个单独的查询,查询总数将超过其阈值.我现在认为简单地从他们放在网上的TXT文件中抓取数据可能更容易,但我在使用R解析数据时遇到问题.这是一个包含数据的示例页面:
http://www.bls.gov/lau/laucnty90.txt#90 = 1990
我最初尝试使用rvest包解析文件.但由于数据都在一个<p>标签中,我认为该工具的表格没有足够的HTML结构.
然后,我尝试download.file和read.table.但同样,这些工具的数据似乎不是正确的格式 - 顶部和底部的额外行,而"分隔字符"只是一个空格,当县名包含空格时会混淆R.
在一天结束时,我只需要一个data.frame,其中包含来自此文件的3个数据:状态FIPS代码,县FIPS代码和失业率.
我现在认为创建此data.frame的最简单方法可能是下载excel文件,删除我不需要的列,删除顶部和底部的额外文本,导出为CSV,然后读取它到R.
我可以在14年中的每一年都这样做.但是我失去了一些可重复性 - 其他人将无法轻易验证我在导入过程中没有犯错误.
有没有人看到更简单的方法来创建这个data.frame?
采用类似示例中的vbulletin网站.我希望能够从线程中删除文本消息.但是,消息的css选择器称为#post_message_xxx,其中xxx是变量id号.
如何将选择器与html_nodes部分匹配,以便我得到所有以#post_message开头的,无论它们如何结束?
或许我应该问一个更普遍的问题.如果我希望能够将作者归属于消息并跟踪消息顺序,我应该如何抓取页面.
谢谢.
library(rvest)
html <- html("http://www.acme.com/forums/new_rules_28429/")
cast <- html_nodes(html, "#post_message_28429")
cast
> <div id="post_message_28429"> Thanks for posting
> this. </div>
>
> attr(,"class")
[1] "XMLNodeSet"
Run Code Online (Sandbox Code Playgroud) 我在网上搜索这个网页:
http://www.falabella.com.pe/falabella-pe/category/cat40536/Climatizacion?navAction=push
Run Code Online (Sandbox Code Playgroud)
我只需要产品中的信息:"品牌","产品名称","价格".

我可以得到它,但我也从其他用户的类似产品的横幅中获取信息.我不需要它.
但是当我转到页面的源代码时,我看不到那些产品.我认为它是通过javascript或其他东西拉出来的:

问题1: 如何在进行网络抓取时阻止此信息? 这增加了我不需要的产品.但是在源代码中看不到这一部分.
问题2:当提取价格"precio1"时,我将其作为第一个元素:"\n\t\t\t\tSubtotal InternetS/. 0"我在代码源中也看不到它.怎么不刮呢?
library(RSelenium)
library(rvest)
#start RSelenium
checkForServer()
startServer()
remDr <- remoteDriver()
remDr$open()
#navigate to your page
remDr$navigate("http://www.falabella.com.pe/falabella-pe/category/cat40536/Climatizacion?navAction=push")
page_source<-remDr$getPageSource()
Climatizacion_marcas1 <- html(page_source[[1]])%>%
html_nodes(".marca") %>%
html_nodes("a") %>%
html_attr("title")
Climatizacion_producto1 <- html(page_source[[1]])%>%
html_nodes(".detalle") %>%
html_nodes("a") %>%
html_attr("title")
Climatizacion_precio1 <- html(page_source[[1]])%>%
html_nodes(".precio1") %>%
html_text()
Run Code Online (Sandbox Code Playgroud) 我似乎无法开始html_table()工作.
这是一个很好的例子:(试图刮掉6场比赛:桌子)
library(rvest)
hockey <- html("http://www.hockey-reference.com/boxscores/2015/3/6/")
hockey %>%
html_nodes("#stats .tooltip , #stats td , #stats a") %>%
html_table()
Run Code Online (Sandbox Code Playgroud)
但我得到了一个html_tag(x) == "table" is not TRUE.它显然是一张桌子.
我怎样才能强迫rvest将节点识别为一个表?
在RI中,您希望从特定类别的Google趋势中获得前10个搜索字词.例如,类别autmotive的前10个搜索字词包含在此网址中:
url <- "https://www.google.com/trends/explore#cat=0-47&geo=US&cmpt=q&tz=Etc%2FGMT-1"
Run Code Online (Sandbox Code Playgroud)
要检索搜索字词,我尝试了以下操作:
library("rvest")
top_searches <- url %>%
read_html() %>%
html_nodes(xpath='//*[@class="trends-bar-chart-name"]') %>%
html_table()
Run Code Online (Sandbox Code Playgroud)
但是,此代码会生成一个空列表(请注意,我使用Selectorgadget来计算'xpath').
我试图通过URL列表来搜索一些IMDB数据.不幸的是,我的输出并不是我所希望的,更不用说将它存储在数据帧中了.
我得到了网址
library(rvest)
topmovies <- read_html("http://www.imdb.com/chart/top")
links <- top250 %>%
html_nodes(".titleColumn") %>%
html_nodes("a") %>%
html_attr("href")
links_full <- paste("http://imdb.com",links,sep="")
links_full_test <- links_full[1:10]
Run Code Online (Sandbox Code Playgroud)
然后我可以得到满意的内容
lapply(links_full_test, . %>% read_html() %>% html_nodes("h1") %>% html_text())
Run Code Online (Sandbox Code Playgroud)
但它是一个嵌套列表,我不知道如何将它放入R中的正确data.frame中.同样,如果我想获得另一个属性,请说
%>% read_html() %>% html_nodes("strong span") %>% html_text()
Run Code Online (Sandbox Code Playgroud)
要检索IMDB评级,我得到相同的嵌套列表输出,最重要的是我必须做两次read_html()...这需要花费很多时间.有一个更好的方法吗?我想for循环,但我无法让它以这种方式工作:(
我刚开始用R的rvest库进行拼抢.太粗体了,我开始查询3206个子页面,每个子页面都要提取一个字符串.
问题是:
持续时间.
我的问题是:
我可以优化我的脚本(下面)以加快执行速度吗?
背景:
仅仅为3个整数循环工作正常,但我的脚本现在已经运行了很长时间.我不知道任何Python(我可能会改变,因为我听说有一个叫做的东西aiohttp).如果没有其他办法,我将不胜感激,如果有人可以提供一个良好的教程或替代解决方案的链接.
脚本
library(rvest)
library(data.table)
#READ IN HTML
#Link: http://globalenergyobservatory.org/list.php?db=PowerPlants&type=Hydro
hydro <- read_html("http://globalenergyobservatory.org/list.php?db=PowerPlants&type=Hydro")
#GET ATTRIBUTES
attributes <- hydro %>%
html_nodes("#list a") %>%
html_attrs()
#WRITE URLs
urls = list()
for (i in 1:3206) {
da <- unlist(attributes[i])
dat <- da[1]
data <- paste("http://www.globalenergyobservatory.org/",dat, sep="")
urls[[i]] <- data
}
#GET ABSTRACTS
abstracts = list()
for(i in 1:3206) {
to_use <- read_html(urls[[i]])
to_use %>%
html_nodes("#Abstract_Block td") %>%
html_text() -> to_write
abstracts[[i]] <- to_write
}
Run Code Online (Sandbox Code Playgroud) 我花了一天时间在互联网上搜索如何做到这一点的例子,但是我仍然在旋转,并且可以使用一点方向.我是html的新手,具有基本的R编码经验,并且对任何其他编码语言的经验很少.
我有一个500多个(可能更多)气象站的列表,我想从这个网站下载FW13格式的数据(https://fam.nwcg.gov/fam-web/kcfast/html/wxhmenu.htm).在浏览器中,您填写表单,提交表单,它将开始将FW13文件下载到我的默认下载文件夹.
我的目标是使用R填写html表单,提交它,然后接受将结果文件下载到定义的位置.表单本身由文本和单选按钮组成.以下是单个查询的示例:
站ID:020207
开课日期:2000-01-01
截止日期:2017-12-31
观察类型:每小时
计划选项:立即运行
我沿着RCurl和rvest包的兔子洞走下去,甚至开始试用rSelenium.我见过的大多数例子都是直接从网站上抓取信息,但我只想接受下载生成的文件.
如果我只能提交单个请求并下载单个文件,我相信我可以弄清楚如何使用站点ID列表来循环,以实现我所需要的.
我为没有任何示例代码而道歉.我所有的试验都是在黑暗中盲目拍摄,我甚至不确定我是否正在使用正确的包装来完成这项任务.任何帮助或方向非常感谢!
我有点卡在这里。我想从网站上抓取数据,并提取一些内容,例如用户评分,评论等。我正在尝试将数据添加到数据框中。
下面是我到目前为止的代码:
# Read html and select the URLs for each game review.
library(rvest)
library(dplyr)
library(plyr)
# Read the webpage and the number of ratings.
getGame <- function(metacritic_game) {
total_ratings<- metacritic_game %>%
html_nodes("strong") %>%
html_text()
total_ratings <- ifelse(length(total_ratings) == 0, NA,
as.numeric(strsplit(total_ratings, " ") [[1]][1]))
# Get the game title and the platform.
game_title <- metacritic_game %>%
html_nodes("h1") %>%
html_text()
game_platform <- metacritic_game %>%
html_nodes(".platform a") %>%
html_text()
game_platform <- strsplit(game_platform," ")[[1]][57:58]
game_platform <- gsub("\n","", game_platform)
game_platform<- paste(game_platform[1], game_platform[2], sep …Run Code Online (Sandbox Code Playgroud)