我已經廣泛地嘗試回答這個問題,但是我似乎無法將任何現有的解決方案適用於我的案例(或者我無法理解如何這樣做)。 林嘗試使用下面的代碼(工作)從R提取特定數據時出錯
##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)
然後我得到以下響應提取API調用數據:
$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"
更新,我已經嘗試過的事情像write.csv,它給我的錯誤: > write.csv(ZomatoData, 「C:/Users/sanaz/Desktop/Zomato.csv」) 錯誤(函數( ...,row.names = NULL,check.rows = FALSE,check.names = TRUE,: 參數意味着不同的行數:1,0 這是可以理解的,因爲我試圖寫入的內容不適合矩形電子表格。這就是爲什麼我卡住了。 –
當你做'長度(ZomatoData $餐館)時,你會得到什麼' –
@Hardikgupta我得到了'[1] 20' –