我想使用 R 抓取以下网站
https://www.domain.com.au/suburb-profile/drummoyne-nsw-2047
我想提取“市场趋势”标题下方表格中的数据
我已经尝试了以下不起作用
url <- "https://www.domain.com.au/suburb-profile/drummoyne-nsw-2047"
page <- read_html(url)
contentnodes <- page %>%
html_nodes("div.css-15dn4s") %>%
html_attr("table") %>%
jsonlite::fromJSON()
提前致谢
需要看中间内容。如果你只是通过你的管道,你可以注意到什么时候出了问题。
contentnodes <- page %>%
html_nodes("div.css-15dn4s")
contentnodes
# {xml_nodeset (0)}
让我们换一种方式。要查找 html 类,我们可以使用
.
前导字符串:
page %>%
html_nodes(".css-15dn4s8")
# {xml_nodeset (1)}
# [1] <table aria-describedby="market-data-context" class="css-15dn4s8">\n<thea ...
page %>%
html_nodes(".css-15dn4s8") %>%
html_table()
# [[1]]
# # A tibble: 7 × 7
# Bed@media (max-width:720px){.css…¹ Type Media…² Avg d…³ Clear…⁴ Sold …⁵ ``
# <chr> <chr> <chr> <chr> <chr> <int> <chr>
# 1 .css-gsqvet{stroke-linejoin:round… House $1.685m 32 days 67% 17 .css…
# 2 3 House $2.242m 41 days 54% 28 Open
# 3 4 House $3.4m 23 days 61% 23 Open
# 4 5 House $3.745m 66 days 77% 15 Open
# 5 1 Unit $740k - - 11 Open
# 6 2 Unit $1.1m 46 days 74% 68 Open
# 7 3 Unit $1.825m 60 days 74% 27 Open
# # … with abbreviated variable names
# # ¹`Bed@media (max-width:720px){.css-1lps80v{position:absolute !important;height:1px;width:1px;overflow:hidden;-webkit-clip:rect(1px 1px 1px 1px);clip:rect(1px 1px 1px 1px);-webkit-clip:rect(1px,1px,1px,1px);clip:rect(1px,1px,1px,1px);}}rooms`,
# # ²`Median price`, ³`Avg days on market`, ⁴`Clearance rate`,
# # ⁵`Sold this year`
这是一个带表格的列表,我们可以使用
`[[`
:提取它
contentnodes <- page %>%
html_nodes(".css-15dn4s8") %>%
html_table()
contentnodes <- contentnodes[[1]]
as.data.frame(contentnodes)
# *** output flushed ***
contentnodes
# # A tibble: 7 × 7
# Bed@media (max-width:720px){.css…¹ Type Media…² Avg d…³ Clear…⁴ Sold …⁵ ``
# <chr> <chr> <chr> <chr> <chr> <int> <chr>
# 1 .css-gsqvet{stroke-linejoin:round… House $1.685m 32 days 67% 17 .css…
# 2 3 House $2.242m 41 days 54% 28 Open
# 3 4 House $3.4m 23 days 61% 23 Open
# 4 5 House $3.745m 66 days 77% 15 Open
# 5 1 Unit $740k - - 11 Open
# 6 2 Unit $1.1m 46 days 74% 68 Open
# 7 3 Unit $1.825m 60 days 74% 27 Open
# # … with abbreviated variable names
# # ¹`Bed@media (max-width:720px){.css-1lps80v{position:absolute !important;height:1px;width:1px;overflow:hidden;-webkit-clip:rect(1px 1px 1px 1px);clip:rect(1px 1px 1px 1px);-webkit-clip:rect(1px,1px,1px,1px);clip:rect(1px,1px,1px,1px);}}rooms`,
# # ²`Median price`, ³`Avg days on market`, ⁴`Clearance rate`,
# # ⁵`Sold this year`