无法从R中提取特定数据

时间:2017-10-19 04:36:16

标签: r api csv

我已经尝试过广泛地回答这个问题但是我似乎无法根据我的情况调整任何现有的解决方案(或者我没有理解如何这样做)。 我试图使用以下代码(有效)从API调用中提取数据

##location details (works)
require(httr)

URL <- 'https://developers.zomato.com/api/v2.1/search?'

request <- GET(URL,
           add_headers(User_key=""),
           query=list(entity_id = '260', 
                      entity_type = 'city'))
content(request)
ZomatoData <-content(request)

然后我得到以下回复:

$restaurants[[13]]
$restaurants[[13]]$restaurant
$restaurants[[13]]$restaurant$R
$restaurants[[13]]$restaurant$R$res_id
[1] 16562670


$restaurants[[13]]$restaurant$apikey
[1] ""

$restaurants[[13]]$restaurant$id
[1] "16562670"

$restaurants[[13]]$restaurant$name
[1] "Home Thai"

$restaurants[[13]]$restaurant$url
[1] "https://www.zomato.com/sydney/home-thai-cbd?
utm_source=api_basic_user&utm_medium=api&utm_campaign=v2.1"

$restaurants[[13]]$restaurant$location
$restaurants[[13]]$restaurant$location$address
[1] "Shop 1-2, 299 Sussex Street, CBD, Sydney"

$restaurants[[13]]$restaurant$location$locality
[1] "CBD"

$restaurants[[13]]$restaurant$location$city
[1] "Sydney"

$restaurants[[13]]$restaurant$location$city_id
[1] 260

$restaurants[[13]]$restaurant$location$latitude
[1] "-33.8744859237"

$restaurants[[13]]$restaurant$location$longitude
[1] "151.2044165656"

$restaurants[[13]]$restaurant$location$zipcode
[1] "2000"

$restaurants[[13]]$restaurant$location$country_id
[1] 14

$restaurants[[13]]$restaurant$location$locality_verbose
[1] "CBD, Sydney"


$restaurants[[13]]$restaurant$switch_to_order_menu
[1] 0

$restaurants[[13]]$restaurant$cuisines
[1] "Thai, Salad"

$restaurants[[13]]$restaurant$average_cost_for_two
[1] 60

$restaurants[[13]]$restaurant$price_range
[1] 3

$restaurants[[13]]$restaurant$currency
[1] "$"

$restaurants[[13]]$restaurant$offers
list()

$restaurants[[13]]$restaurant$user_rating
$restaurants[[13]]$restaurant$user_rating$aggregate_rating
[1] "4.5"

这一切都很好但我希望只提取用户评级聚合评级值并将其写入CSV文件,使其有2列,1为餐厅名称,另一列为评级。 想知道是否有人可以提供帮助? 非常感谢任何善意和有用的回应 更新:这是str(ZomatoData)的输出

> str(ZomatoData)
List of 4
$ results_found: int 16056
$ results_start: int 0
$ results_shown: int 20
$ restaurants  :List of 20
 ..$ :List of 1
.. ..$ restaurant:List of 23
.. .. ..$ R                   :List of 1
.. .. .. ..$ res_id: int 16564875
.. .. ..$ apikey              : chr "api-key"
.. .. ..$ id                  : chr "16564875"
.. .. ..$ name                : chr "The Grounds of Alexandria Cafe"
.. .. ..$ url                 : chr "https://www.zomato.com/sydney/the-
grounds-of-alexandria-cafe-alexandria?
utm_source=api_basic_user&utm_medium=ap"| __truncated__
.. .. ..$ location            :List of 9
.. .. .. ..$ address         : chr "Shop 7A, 2 Huntley Street, Alexandria, 
Sydney"
.. .. .. ..$ locality        : chr "The Grounds of Alexandria, Alexandria"
.. .. .. ..$ city            : chr "Sydney"
.. .. .. ..$ city_id         : int 260
.. .. .. ..$ latitude        : chr "-33.9110760390"
.. .. .. ..$ longitude       : chr "151.1936605722"
.. .. .. ..$ zipcode         : chr "2015"
.. .. .. ..$ country_id      : int 14
.. .. .. ..$ locality_verbose: chr "The Grounds of Alexandria, Alexandria, 
Sydney"
.. .. ..$ switch_to_order_menu: int 0
.. .. ..$ cuisines            : chr "Cafe, Coffee and Tea, Salad"
.. .. ..$ average_cost_for_two: int 80
.. .. ..$ price_range         : int 3
.. .. ..$ currency            : chr "$"
.. .. ..$ offers              : list()
.. .. ..$ thumb               : chr 

.. .. ..$ user_rating         :List of 4
.. .. .. ..$ aggregate_rating: chr "4.6"
.. .. .. ..$ rating_text     : chr "Excellent"
.. .. .. ..$ rating_color    : chr "3F7E00"
.. .. .. ..$ votes           : chr "3162"

1 个答案:

答案 0 :(得分:0)

我无法重现您上面的示例代码,并且您没有提供最小的示例数据集,因此我无法对您的数据进行测试。

除此之外,您可以尝试以下方法:

df <- do.call(rbind.data.frame,
    lapply(ZomatoData$restaurants, function(x) {
        c(x$restaurant$name, x$restaurant$user_rating$aggregate_rating) } ) );
write.csv(df, file = "summary.csv");

此处restaurants应该是包含您提供的结构的列表。