我正在从汽车拍卖网站上搜集和分析数据。我的目标是开发日期时间和情绪分析技能,我喜欢旧车。该网站是Bring A Trailer--他们不提供API访问(我问过),但robots.txt没问题。
SO用户' 42'指出这是BAT的条款不允许的,所以我删除了他们的基本网址。我可能会删除这个问题。在考虑之后,我可以通过从浏览器中保存几个网页并分析这些数据来做我想做的事情。我不需要所有的拍卖,我只是按照了一个教程,在这里我正在阅读TOS而不是做我想做的事情......
有些数据很容易被访问,但最好的部分很难,而且我坚持这一点。我真的在寻找关于我的方法的建议。
我的第一步工作:我可以找到并在本地缓存网页:
# Step 3: Process each auction info into df ----------------------------
files <- dir(data_dir, pattern = "*", full.names = TRUE)
# Function: get_auction_details, to be applied to each auction page
get_auction_details <- function(file) {
pagename <- basename(file) # the filename of the page (trailing index for multiples)
page <- read_html(file) # read the html into R ( consider , options = "NOCDATA")
# Grab the title of the auction stored in the ".listing-post-title" tag on the page
title <- page %>% html_nodes(".listing-post-title") %>% html_text()
# Grab the "BAT essentials" of the auction stored in the ".listing-essentials-item" tag on the page
essence <- page %>% html_nodes(".listing-essentials-item") %>% html_text()
# Assemble into a data frame
info_tbl0 <- as_tibble(essence)
info_tbl <- add_row(info_tbl0, value = title, .before = 1)
names(info_tbl) [1] <- pagename
return(info_tbl)
}
# Apply the get_auction_details function to each element of files
bat0 <- map_df(files, get_auction_details) # run function
bat <- gather(bat0) %>% subset(value != "NA") # serialize results
# Save as csv
write_csv(bat, path = "data-csv/bat04.csv") # this table contains the expected metadata:
key,value
1931-ford-model-a-12,Modified 1931 Ford Model A Pickup
1931-ford-model-a-12,Lot #8576
1931-ford-model-a-12,Seller: TargaEng
我还可以从这些缓存页面处理有关拍卖的元数据,使用SelectorGadget识别css选择器并将它们指定为rvest:
<script type='text/javascript'>
/* <![CDATA[ */
var BAT_VMS = { ...bids, comments, results
/* ]]> */
</script>
但拍卖数据(出价,评论)位于CDATA部分内:
tmp <- page %>% html_nodes(".comments-list") %>% html_text()
我已尝试使用我使用SelectorGadget找到的路径尝试此部分中的元素,但找不到它们 - 这会给出一个空列表:
{{1}}
查看此CDATA部分中的文本,我看到了一些xml标记,但它没有像我在检查实时网页的拍卖部分时那样在缓存文件中构建。
要提取此信息,我是否应该尝试解析信息&#34; as-is&#34;从这个CDATA部分,或者我可以转换它,以便它可以解析像XML?还是我在错误的树上吠叫?
我感谢任何建议!
答案 0 :(得分:0)
它埋在XML2文档中,但是您可以使用此选项保持CDATA不变。
# Instead of rvest::read_html()
xml2::read_xml(options = "NOCDATA")
以这种方式阅读提要之后,您将能够以所需的方式访问评论列表。
tmp <- page %>% html_nodes(".comments-list") %>% html_text()