CSS选择器和XPath方法都是用来定位DOM树的标签,只不过两者的定位表示形式上存在一些差别:

  • CSS 方法提取节点
library("rvest")
single_table_page <- read_html("single-table.html")
# 提取url里的所有表格
html_table(single_table_page)
html_table(html_node(single_table_page,"table"))
products_page <- read_html("./case/products.html")
products_page %>% html_nodes(".product-list li .name") %>% html_text()
product_items <- products_page %>% html_nodes(".product-list li")
data.frame(name = product_items %>% html_nodes(".name") %>% html_text(),
price = product_items %>% html_nodes(".price") %>%html_text()
%>% str_replace_all(pattern="\\$",replacement="") %>%
as.numeric(), stringsAsFactors = FALSE)
  • XPath 方法提取节点
page <- read_html("./case/new-products.html")
#查找所有p节点
page %>% html_nodes(xpath="//p")
#CSS's way
page %>% html_nodes("p")
# 找到所有具有class属性的li标签
# xpath's way
page %>% html_nodes(xpath="//li[@class]")
# CSS's way
page %>% html_nodes("li[class]")
# 找到id=‘list’的div标签下的所有li标签
# xparth's way
page %>% html_nodes(xpath="//div[@id='list']/ul/li")
# CSS's way
page %>% html_nodes("div#list > ul > li")
# 查找包含p节点的所有div节点
page %>% html_nodes(xpath="//div[p]")
# 查找所有class值为“info-value”,文本内容为“Good”的span节点
page %>% html_nodes(xpath = "//span[@class='info-value' and text()='Good']")

R语言爬虫:CSS方法与XPath方法对比(代码实现)的更多相关文章

  1. R语言爬虫:CSS方法与XPath方法对比(表格介绍)

    css 选择器与 xpath 用法对比 目标 匹配节点 CSS 3 XPath 所有节点 ~ * //* 查找一级.二级.三级标题节点 <h1>,<h2>,<h3> ...

  2. R语言爬虫初尝试-基于RVEST包学习

    注意:这文章是2月份写的,拉勾网早改版了,代码已经失效了,大家意思意思就好,主要看代码的使用方法吧.. 最近一直在用且有维护的另一个爬虫是KINDLE 特价书爬虫,blog地址见此: http://w ...

  3. 给社团同学做的R语言爬虫分享

    大家好,给大家做一个关于R语言爬虫的分享,很荣幸也有些惭愧,因为我是一个编程菜鸟,社团里有很多优秀的同学经验比我要丰富的多,这次分享是很初级的,适用于没有接触过爬虫且有一些编程基础的同学,内容主要有以 ...

  4. 简单R语言爬虫

    R爬虫实验 R爬虫实验 PeRl 简单的R语言爬虫实验,因为比较懒,在处理javascript翻页上用了取巧的办法. 主要用到的网页相关的R包是: {rvest}. 其余的R包都是常用包. libra ...

  5. R语言︱文件读入、读出一些方法罗列(批量xlsx文件、数据库、文本txt、文件夹)

    笔者寄语:小规模的读取数据的方法较为简单并且多样,但是,批量读取目前看到有以下几种方法:xlsx包.RODBC包.批量转化成csv后读入. R语言中还有一些其他较为普遍的读入,比如代码包,R文件,工作 ...

  6. R 语言爬虫 之 cnblog博文爬取

    Cnbolg Crawl a). 加载用到的R包 ##library packages needed in this case library(proto) library(gsubfn) ## Wa ...

  7. R语言读取EXCEL文件的各种方法

    路径问题 原始文件路径C:\Users\air\Desktop\1.txt R中有两种方法读取该路径 C:\\Users\\air\\Desktop\\1.txt C:/Users/air/Deskt ...

  8. R语言爬虫:爬取包含所有R包的名称及介绍

    第一种方法 library("rvest") page <- read_html("https://cran.rstudio.com/web/packages/av ...

  9. R语言爬虫 rvest包 html_text()-html_nodes() 原理说明

    library(rvest) 例子网页:http://search.51job.com/jobsearch/search_result.php?fromJs=1&jobarea=010000% ...

随机推荐

  1. 一个U盘黑掉你:TEENSY实战

    从传统意义讲,当你在电脑中插入一张CD/DVD光盘,或者插入一个USB设备时,可以通过自动播放来运行一个包含恶意的文件,不过自动播放功能被关闭时,autorun.inf文件就无法自动执行你的文件了.然 ...

  2. PyCharm的Debug工具栏中的Watches

    In the Watches pane you can evaluate any number of variables or expressions in the context of the cu ...

  3. CentOS下go 安装

     go 语言源码安装依赖 ,gcc ,make glibc库,等,上述工具安装省略,另外,其源代码更新采用的是mercurial 工具,安装前先安装mercureal :   1.mercurial安 ...

  4. UVa 247 - Calling Circles(Floyd求有向图的传递闭包)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  5. Gluon Data API

    http://mxnet.apache.org/api/python/gluon/data.html import sys import os import time import mxnet as ...

  6. Myeclipse 自带Tomcat启动8080端口占用

    在启动Myeclipse自带的Tomcat发现报错,显示8080端口被占用 第一步:window+r 组合键,调出命令窗口. 第二步:输出命令:netstat -ano|findstr 8080   ...

  7. windows快捷命令修炼

    Description Windows Key combination Open/Close the Start Menu Windows key Open the Action center. Wi ...

  8. 解决vue-router中this.$router.push无法在新窗口中打开

    解决vue-router中this.$router.push无法在新窗口中打开 let routeData = this.$router.resolve({ path: '/consult', que ...

  9. Junit报错Initialization Error

    出错原因是没有把方法声明为public

  10. async--ajax异步传输

    如图:轨迹点组编号依赖所属路口的设置. 所以ajax是异步执行,获取到所属路口还没执行完,就已经执行到根据所述路口获取轨迹点组编号,所以获取不到轨迹点组编号. async:false改成同步执行.就没 ...