Comments (7)
Alternatively, you could write it like this:
nodes <- "http://pyvideo.org/category/50/pycon-us-2014" %>%
html %>%
html_nodes("div.video-summary-data")
column <- function(x) nodes %>% html_node(xpath = x) %>% html_text()
df <- data.frame(
title = column("div[1]//a"),
author = column("div[3]//a"),
date = column("div[4]//small[1]"),
language = column("div[4]//small[2]"),
description = column("div[5]//p"),
stringsAsFactors = FALSE
)
I think this is simple enough that you don't need a wrapper
from rvest.
Man this is amazing!! Soon people will see how powerful r can be for webscraping
from rvest.
I like it - I need to think through the syntax a bit though. It's a bit too reliant on xpath currently - i.e. there's no way to access an attribute with a css selector. It'd also be nice if it worked in a where you could easily test each component, before building up into a data frame.
(Also I think this will require a variant of html_node()
that doesn't unlist()
, and assumes the selector only pulls out one node. Now that I think of it, html_node()
is basically [
, and we need an equivalent of [[
. Maybe html_node()
should be renamed to html_nodes()
and html_node()
should only ever pull out one node per input element.)
from rvest.
Another idea is to implement the css selector extensions used by https://github.com/EricChiang/pup
from rvest.
Great thinking! The design of html_node()
and html_nodes()
works with many problematic cases in which the nodes are not quite regular. The code above does not really work due to some exceptional cases in the webpage, but with these two functions, it works correctly without too much worry about the missing/blank values in some fields.
from rvest.
Using html_node()
and html_nodes()
, the code can be much less error-prone.
library(rvest)
nodes <- "http://pyvideo.org/category/50/pycon-us-2014" %>%
html %>%
html_nodes("div.video-summary-data")
cols <- list(
title = "div[1]//a//text()",
author = "div[3]//a//text()",
date = "div[4]//small[1]//text()",
language = "div[4]//small[2]//text()",
description = "div[5]//p//text()")
df <- cols %>%
lapply(function(col) {
nodes %>%
html_node(xpath = col) %>%
html_text
}) %>%
data.frame(stringsAsFactors = FALSE)
from rvest.
@hadley, fantastic use, just forget about using function :) This workflow looks very natural now.
from rvest.
Related Issues (20)
- rvest fails to parse HTML page from google scholar; returns `xml_nodeset (0)` HOT 2
- Create `read_html()` documentation page HOT 1
- Some way to customise user agent for `read_html_live()`
- Apparent typo in `read_html_live()` docs
- Github Actions Can't Install Dev Version HOT 4
- Figure out dynamic html elements selection
- read_html_live() practical implementation HOT 1
- Release rvest 1.0.4
- Long lines truncated at 10,000,000 chars. HOT 4
- Invalid Char in Json Text
- `req_perform_iteratively()` + `paths` HOT 1
- Need absolute URL helper HOT 1
- Web scraping vignette
- LiveHTML object corrupted after `$click()` HOT 1
- read_html should take httr2 response directly
- Hide more automated browser tells
- read_html_live() memory "leak" HOT 4
- Example broken in LiveHTML page HOT 1
- README should end with link to getting started vignette
- Select option from list using htmlLIVE
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rvest.