Web scraping of image

我是研究僧i 提交于 2019-12-03 20:42:17

You need to specify which attribute you want to extract as a parameter for html_attr. Also, you may want to make your CSS selector, the parameter for html_node, more specific. Here is my code:

library(rvest)

UrlPage <- html ("http://eyeonhousing.org/2012/11/gdp-growth-in-the-third-quarter-improved-but-still-slow/")
ImgNode <- UrlPage %>% html_node("img.wp-image-5984")
link <- html_attr(ImgNode, "src")

The link variable now contains the URL.

You can find a decent reference for css selectors here: http://www.w3schools.com/cssref/css_selectors.asp

Also the rvest documentation has some good examples on how to use its functions: http://cran.r-project.org/web/packages/rvest/rvest.pdf

klib is right. just updated html (deprecated) to read_html and added a download command.

library(rvest)    

myurl <- read_html ("http://eyeonhousing.org/2012/11/gdp-growth-in-the-third-quarter-improved-but-still-slow/")
mynode <- myurl %>% html_node("img.wp-image-5984")
link <- html_attr(mynode, "src")
download.file(url = link,destfile = "test.jpg")
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!