我正在尝试将数据另存为“csv”格式文件或从类似于以下内容的solr查询中提取为数据框:
http://localhost:8983/solr/techproducts/select?q=ipod&fl=id,cat,name,popularity,price,score&wt=csv
我得到一个作为网页输出的响应,如下所示:
我能够从html文件(网页solr输出)中显示的csv输出中手动复制,粘贴,保存和检索csv格式的数据。但是,我无法使用R来自动化该过程。我既无法通过
unlist
解析和获取整个内容-按正确的顺序排列输出,也无法提取并将网页中显示的内容另存为csv文件。我徒劳地尝试了solrium
和httr
软件包。我也尝试在Windows PowerShell中使用Export-Csv
选项将数据保存为csv文件格式,但没有任何运气。请求建议以解决该问题。
最佳答案
这没有解决有关powershell的任何特定问题,但是首先使用solrium
可以做到这一点,如果没有解决,它怎么会失败:
(要完全复制),设置Solr
cd solr-6.6.0
bin/solr start -e cloud -noprompt
bin/solr create -c techproducts -d sample_techproducts_configs
bin/post -c techproducts example/exampledocs/*.xml
在R中:
solr_connect()
(df <- solr_search(
name = "techproducts",
q = "ipod",
fl = c("id", "cat", "name", "popularity", "price", "score"),
wt = "csv"))
#> # A tibble: 3 x 6
#> id cat name popularity price score
#> * <chr> <chr> <chr> <int> <dbl> <dbl>
#> 1 IW-02 electronics,connector iPod & iPod Mini USB 2.0 Cable 1 11.50 3.2388113
#> 2 F8V7067-APL-KIT electronics,connector Belkin Mobile Power Cord for iPod w/ Dock 1 19.95 2.3162508
#> 3 MA147LL/A electronics,music Apple 60 GB iPod with Video Playback Black 10 399.00 0.9044058
write.csv(df, "myfile.csv", row.names = FALSE)
readLines("myfile.csv")
#> [1] "\"id\",\"cat\",\"name\",\"popularity\",\"price\",\"score\""
#> [2] "\"IW-02\",\"electronics,connector\",\"iPod & iPod Mini USB 2.0 Cable\",1,11.5,3.2388113"
#> [3] "\"F8V7067-APL-KIT\",\"electronics,connector\",\"Belkin Mobile Power Cord for iPod w/ Dock\",1,19.95,2.3162508"
#> [4] "\"MA147LL/A\",\"electronics,music\",\"Apple 60 GB iPod with Video Playback Black\",10,399,0.9044058"
关于r - 如何使用R或PowerShell提取solr查询输出显示的csv输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45555009/