{"rsdb":{"rid":"352314","subhead":"","postdate":"0","aid":"250037","fid":"91","uid":"1","topic":"1","content":"
\n

\u4e00\u822c\u6210\u719f\u7684\u7f51\u7ad9\u90fd\u4f1a\u6709\u53cd\u722c\u866b\u7b56\u7565\uff0c\u4f8b\u5982\u9650\u5236\u8bbf\u95ee\u6b21\u6570\uff0c\u9650\u5236\u8bbf\u95ee IP\uff0c\u52a8\u6001\u663e\u793a\u6570\u636e\u7b49\u3002\u722c\u866b\u548c\u53cd\u722c\u866b\u5c31\u662f\u4e00\u76f4\u76f8\u7231\u76f8\u6740\u5730\u4e92\u76f8\u94b3\u5236\u3002\u5982\u679c\u8981\u901a\u8fc7\u722c\u866b\u6765\u83b7\u53d6\u67d0\u4e9b\u5927\u578b\u7f51\u7ad9\u7684\u6570\u636e\uff0c\u90a3\u662f\u4e00\u4ef6\u5f88\u8d39\u65f6\u8d39\u529b\u7684\u6d3b\u3002\u5c0f\u767d\u603b\u906d\u9047\u8fc7\u5728\u8d9f\u8fc7\u5404\u79cd\u5751\u4e4b\u524d\u5c31\u88ab\u5c01 IP \u6216\u5c01\u8d26\u53f7\u7684\u6253\u51fb\uff08\u545c\u545c~\u8bf4\u7684\u5c31\u662f\u6211\uff09\u3002<\/p> \n

\u4e0d\u8fc7\u6709\u4e00\u4e9b\u516c\u53f8\u5fc3\u6000\u5f00\u653e\u4e92\u8054\u7684\u6001\u5ea6\uff0c\u53cb\u597d\u5730\u7ed9\u5927\u5bb6\u63d0\u4f9b\u4e86 api \u63a5\u53e3\u3002\u8fd9\u4e00\u7bc7\u535a\u5ba2\u5c06\u4ee5\u8c46\u74e3\u7535\u5f71\u4e3a\u4f8b\u8bb0\u5f55\u5982\u4f55\u7ad9\u5728\u5de8\u4eba\uff08api\uff09\u7684\u80a9\u8180\u4e0a\u83b7\u53d6\u6570\u636e\u3002\u4e0d\u8fc7\u8c46\u74e3 api \u73b0\u5728\u8c8c\u4f3c\u4e5f\u9010\u6e10\u6536\u8d77\u6765\u4e86\uff0capi key \u4e5f\u505c\u6b62\u7533\u8bf7\u4e86\uff0c\u4e14\u7528\u4e14\u73cd\u60dc\u4e86\u3002

<\/p> \n

>> Required Packages<\/h4> \n
\n
library(httr)\nlibrary(jsonlite)<\/code><\/pre> \n 


<\/p> \n

>> Get JSON<\/h4> \n
\n

\u8c46\u74e3\u7535\u5f71\u7684 url \u8fd8\u662f\u5f88\u89c4\u5f8b\uff0c\u6bd4\u5982\u8bf4 id \u4e3a 27074316 \u7684\u7535\u5f71\u7684 url \u5c31\u662f https:\/\/movie.douban.com\/subject\/27074316<\/a> \uff0c \u4f46\u662f id \u7684\u6392\u5e8f\u5e76\u4e0d\u662f\u89c4\u5f8b\u7684\u3002\u4f8b\u5982\u4e00\u90e8 2000 \u5e74\u4e0a\u6620\u7684\u7535\u5f71\u7684 id \u53ef\u80fd\u4f1a\u5927\u4e8e 2010 \u5e74\u4e0a\u6620\u7684\u7535\u5f71\u7684 id\u3002<\/p> \n

\u8c46\u74e3\u7535\u5f71\u7684 id \u8303\u56f4\u5927\u7ea6\u4e3a 1300000 \u5230 27200000\uff0c\u4f46\u5176\u4e2d\u53ef\u80fd\u4f1a\u6709\u4e00\u90e8\u5206\u65e0\u6548\u7684 id\u3002\u5982\u679c\u9700\u8981\u83b7\u53d6\u6240\u6709\u8c46\u74e3\u7535\u5f71\u7684\u6570\u636e\uff0c\u90a3\u4e48\u5c31\u8981\u904d\u5386\u6240\u6709 id\u3002\u5982\u679c\u53ea\u662f\u9700\u8981\u4e00\u90e8\u5206\u7684\u6570\u636e\uff0c\u5c31\u8981\u7528\u5230 search key\u3002<\/p> \n

\u6bd4\u5982 id \u4e3a 27074316 \u7684\u7535\u5f71\u7684 api resource url \u662f
https:\/\/api.douban.com\/v2\/movie\/subject\/27074316?apikey={your_api_key}<\/a><\/p> \n

\u6bd4\u5982\u641c\u7d22 2018\u5e74\u7535\u5f71\u7684 api resource url \u662f
https:\/\/api.douban.com\/\/v2\/movie\/search?q=2018?apikey={your_api_key}<\/a><\/p> \n

\u5728\u6d4f\u89c8\u5668\u4e2d\u8f93\u5165\u4e0a\u8ff0 url (\u8bb0\u5f97\u66ff\u6362\u4e3a\u4f60\u7684 api key) \u662f\u53ef\u4ee5\u770b\u89c1\u8fd4\u56de\u7684 json \u7684\uff0c\u800c\u5728 R \u4e2d\u8981\u83b7\u53d6 json \u5c31\u8981\u7528\u5230 package httr \u4e2d\u7684 GET \u51fd\u6570\u3002\u5982\u679c\u8fd4\u56de 200 \u5219\u8868\u793a\u8bf7\u6c42\u6210\u529f\u3002<\/p> \n

url <- https:\/\/api.douban.com\/v2\/movie\/subject\/27074316\nfilm_json <- GET(url, query = list(apikey="your_api_key"))\nif(status_code(film_json) == 200)\n    message("success\\n")<\/code><\/pre> \n 


<\/p> \n

>> Get Text<\/h4> \n
\n

\u83b7\u53d6 json \u540e\u8981\u8bfb\u53d6\u91cc\u9762\u9700\u8981\u7684\u5185\u5bb9\uff0c\u4f8b\u5982\u83b7\u53d6\u7535\u5f71\u540d\u79f0\uff0c\u7535\u5f71\u5bfc\u6f14\u7b49\uff1a<\/p> \n

film_data <- fromJSON(content(film_json, "text"))\ntitle <- film_data$title\ndirector <- film_data$directors$name %>% paste(collapse=",")<\/code><\/pre> \n 

json \u5728\u6d4f\u89c8\u5668\u4e2d\u7684\u6587\u5b57\u90fd\u662f\u6324\u6210\u4e00\u56e2\u7684\u5f88\u4e0d\u597d\u8fa8\u8ba4 key-value pairs\u3002\u5728 R console \u4e2d\u6267\u884c\u4e0b\u9762\u7684\u4ee3\u7801\u5c31\u53ef\u4ee5\u6e05\u6670\u770b\u5230\u5404\u7ec4\u5408\u4e86\uff1a<\/p> \n

toJSON(fromJSON(content(film_json, "text")), pretty=TRUE)<\/code><\/pre> \n 


<\/p> \n

>> Loop over IDs<\/h4> \n
\n

\u4e0a\u9762\u6709\u63d0\u5230\u8bf4\u5982\u679c\u8981\u83b7\u53d6\u5168\u90e8\u6570\u636e\u90a3\u4e48\u6211\u4eec\u5c31\u8981\u5c1d\u8bd5\u904d\u5386\u6240\u6709 id\u3002\u76ee\u524d\u8c46\u74e3\u5bf9\u4e8e api \u7684\u8bbf\u95ee\u9891\u7387\u662f\u6709\u9650\u5236\u7684\uff0c\u6211\u4e86\u89e3\u5230\u7684\u662f\u6bcf\u5206\u949f\u4e0d\u8d85\u8fc7 40 \u6b21\u3002\u6240\u4ee5\u5728\u4e24\u6b21\u8bbf\u95ee\u4e4b\u95f4\u8981\u8bbe\u7f6e\u5408\u7406\u7684\u65f6\u95f4\u5dee\u3002<\/p> \n

\u800c\u4e14\u5e76\u4e0d\u662f\u6bcf\u4e2a id \u90fd\u4e00\u5b9a\u6709\u5bf9\u5e94\u7684\u9875\u9762\uff0c\u56e0\u6b64\u5728\u904d\u5386\u7684\u8fc7\u7a0b\u4e2d\u8fd8\u8981\u5254\u9664\u65e0\u6548 id\u3002\u8fd9\u65f6\u5019\u5c31\u53ef\u4ee5\u7528\u4e0a tryCatch \u6cd5\u5668\u4e86\u3002<\/p> \n

basic_url <- "https:\/\/api.douban.com\/v2\/movie\/subject\/"\nfilm <- data.frame()\nfor (ii in 1:100){ #just for example not real id\n  Sys.sleep(runif(1,1,2)) # set time interval\n  tryCatch(\n    {\n      url <- paste0(basic_url, ii)\n      film_json <- GET(url, query = list(apikey="your_api_key"))\n      if(status_code(film_json) != 200)\n        next\n      film <- rbind(film, getFilmInfo(film_json,ii)) #user-defined function\n    },\n    error = function(cond) {\n      message("Here's the original error message:")\n      message(cond)\n      message("\\n")\n    }\n  )\n}<\/code><\/pre> \n 



\u4ee5\u4e0a\u53ea\u662f\u629b\u7816\u5f15\u7389\uff0c\u800c\u9664\u4e86\u8c46\u74e3\u4ee5\u5916\u8fd8\u6709\u5f88\u591a\u53ef\u7528\u7684 api \uff08\u53ef\u4ee5\u4e0a Github \u67e5\u4e00\u67e5\uff09\uff0c\u591a\u52a0\u5229\u7528\u54e6\uff0c\u6570\u636e\u5c31\u662f\u751f\u4ea7\u529b \uff01<\/p> \n<\/div>","orderid":"0","title":"\u83b7\u53d6\u8c46\u74e3\u7535\u5f71\u6570\u636e\uff08R\u4e0eAPI\u83b7\u53d6\u7f51\u9875\u6570\u636e\uff09","smalltitle":"","mid":"0","fname":"R\u8bed\u8a00","special_id":"0","bak_id":"0","info":"0","hits":"376","pages":"1","comments":"0","posttime":"2019-09-04 01:00:11","list":"1567530011","username":"admin","author":"","copyfrom":"","copyfromurl":"","titlecolor":"","fonttype":"0","titleicon":"0","picurl":"https:\/\/www.cppentry.com\/upload_files\/","ispic":"0","yz":"1","yzer":"","yztime":"0","levels":"0","levelstime":"0","keywords":"
\u83b7\u53d6<\/A> \u7535\u5f71<\/A> \u6570\u636e<\/A> API<\/A> \u7f51\u9875<\/A>","jumpurl":"","iframeurl":"","style":"","template":"a:3:{s:4:\"head\";s:0:\"\";s:4:\"foot\";s:0:\"\";s:8:\"bencandy\";s:0:\"\";}","target":"0","ip":"120.229.33.222","lastfid":"0","money":"0","buyuser":"","passwd":"","allowdown":"","allowview":"","editer":"","edittime":"0","begintime":"0","endtime":"0","description":"\u83b7\u53d6\u8c46\u74e3\u7535\u5f71\u6570\u636e\uff08R\u4e0eAPI\u83b7\u53d6\u7f51\u9875\u6570\u636e\uff09","lastview":"1715493259","digg_num":"0","digg_time":"0","forbidcomment":"0","ifvote":"0","heart":"","htmlname":"","city_id":"0"},"page":"1"}