
问题是我无法弄清楚如何将scrape结果存储到“增长列表”中,然后我想将其制作成一个表并最后保存到.csv文件中.我只能一次刮掉一个县,然后重写自己.
有什么想法吗? (相当新的R和刮擦一般)
i <- 1for (i in 1:255) { d1 <- as.character(TX_countIEs[i,1]) uri.seed <- paste(c('http://www.tax-rates.org/texas/',d1,'_county_property_tax'),collapse='') HTML <- HTMLTreeParse(file = uri.seed,isURL=TRUE,useInternalNodes = TRUE) avg_taxrate <- sapply(getNodeSet(HTML,"//div[@class='Box']/div/div[1]/i[1]"),xmlValue) t1 <- data.table(d1,avg_taxrate) i <- i+1}write.csv(t1,"2015_TX_PropertyTaxes.csv")解决方法 这使用了rvest,提供了一个进度条,并利用了页面上已经存在URL的事实: library(rvest)library(pbapply)pg <- read_HTML("http://www.tax-rates.org/texas/property-tax")# get all the county tax table linksctys <- HTML_nodes(pg,"table.propertyTaxtable > tr > td > a[href*='county_property']")# match your lowercased namescounty_name <- tolower(gsub(" County","",HTML_text(ctys)))# spIDer each page and return the rate %county_rate <- pbsapply(HTML_attr(ctys,"href"),function(URL) { cty_pg <- read_HTML(URL) HTML_text(HTML_nodes(cty_pg,xpath="//div[@class='Box']/div/div[1]/i[1]"))},USE.nameS=FALSE)tax_table <- data.frame(county_name,county_rate,stringsAsFactors=FALSE)tax_table## county_name county_rate## 1 anderson Avg. 1.24% of home value## 2 andrews Avg. 0.88% of home value## 3 angelina Avg. 1.35% of home value## 4 aransas Avg. 1.29% of home valuewrite.csv(tax_table,"2015_TX_PropertyTaxes.csv") 注1:我限制刮到4不会杀死提供免费数据的站点的带宽.
注意2:该网站上只有254个县的税收链接,所以如果你有255个,你似乎还有一个额外的.
总结以上是内存溢出为你收集整理的使用循环通过Web抓取创建表全部内容,希望文章能够帮你解决使用循环通过Web抓取创建表所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)