Webscraping with R JSON

前端 未结 1 877
迷失自我
迷失自我 2021-01-26 15:47

I want to get the names of the companies by two columns Region and Name of role-player. I find json links on each page already, b

相关标签:
1条回答
  • 2021-01-26 16:36

    My personal preference for this is to use the library jsonlite and not use fromJSON at all

    require(jsonlite)
    data<-jsonlite::fromJSON(raw_data, simplifyDataFrame = TRUE)
    finalData<-data.frame(cbind(data$rolePlayers$RolePlayer$orgName, data$rolePlayers$Region$RegionNameEng))
    colnames(finalData)<-c("Name", "Region")  
    

    Which gives you the following data frame:

                                       Name       Region
                    GoodHope Cheese (Pty) Ltd Western Cape
                           Jay Chem (Pty) Ltd Western Cape
                    Coltrade International cc Western Cape
     GC Rieber Compact South Africa (Pty) Ltd Western Cape
                        Latana Cheese Pty Ltd Western Cape
                           Marco Frischknecht Western Cape
    

    A great way to visualize how to query and what is in your JSON string can be found here:Chris Photo JSON viewer

    You can just cut and paste it in there from the raw_data (removing external quotation marks). From there it becomes easy to see how to structure your data using addressing like you would with a traditional data frame and the $ operator.

    0 讨论(0)
提交回复
热议问题