r - 从网页中提取表格
问题描述
问题
我正在尝试下载以下网页中的表格: https ://www.ato.gov.au/Rates/Individual-income-tax-for-prior-years/
我的尝试
read_html('https://www.ato.gov.au/Rates/Individual-income-tax-for-prior-years/') %>%
html_nodes(xpath = '//tr//*[(((count(preceding-sibling::*) + 1) = 1) and parent::*)]') %>%
html_text()
问题是此代码返回 639 行数据。我希望导入的数据能够维护其在网页上的表结构(例如表列表甚至是一个大数据框)。
解决方案
我建议将它们保留为数据框列表并区分表,用caption
可用的名称命名它们
library(dplyr)
library(rvest)
url <- "https://www.ato.gov.au/Rates/Individual-income-tax-for-prior-years/"
url %>%
read_html() %>%
html_table() %>%
setNames(., url %>%
read_html() %>%
html_nodes("caption") %>%
html_text())
#$`Resident tax rates for 2016-17`
# Taxable income Tax on this income
#1 0 – $18,200 Nil
#2 $18,201 – $37,000 19c for each $1 over $18,200
#3 $37,001 – $87,000 $3,572 plus 32.5c for each $1 over $37,000
#4 $87,001 – $180,000 $19,822 plus 37c for each $1 over $87,000
#5 $180,001 and over $54,232 plus 45c for each $1 over $180,000
#$`Resident tax rates for 2015-16`
# Taxable income Tax on this income
#1 0 – $18,200 Nil
#2 $18,201 – $37,000 19c for each $1 over $18,200
#3 $37,001 – $80,000 $3,572 plus 32.5c for each $1 over $37,000
#4 $80,001 – $180,000 $17,547 plus 37c for each $1 over $80,000
#5 $180,001 and over $54,547 plus 45c for each $1 over $180,000
#......
如果您希望它作为一个单独的数据框,我们可以使用bind_rows
with.id
参数
url %>%
read_html() %>%
html_table() %>%
setNames(., url %>%
read_html() %>%
html_nodes("caption") %>%
html_text()) %>%
bind_rows(.id = "id")
推荐阅读
- sql - 带有索引的postgresql jsonb不区分大小写查询
- sql - 查询列上没有值的特定关系
- sql - 在 Oracle SQl 中拆分数据字段字符串值
- angular - 如何设置在Angular中构建期间打包文件的顺序
- r - 使用 R 在元分析中结合森林图和红绿灯图
- react-native - React-Native App 在 expo 上完美运行,但在发布后它显示出看不见的行为
- python - Python 日志记录失败,网络驱动器上的日志文件(Windows 10)
- php - Keycloak-传递参数以满足密码策略要求
- c++ - 使用协作组网格同步()函数时,CUDA 内核停止工作
- php - Hello World PHP 脚本的奇怪 php 输出