问题
I am trying to find the sentiment of "I am not joking" (For Example) using viral Heat API using R programming. I used getURL in RCurl package as shown below:
getURL("https://www.viralheat.com/api/sentiment/review.json?text=i%20am%20happy&api_key=", ssl.verifypeer=FALSE)
This is working fine in Google Chrome. But in IE it is getting redirected to "https://www.viralheat.com/browsers" as the ViralHeat API is incapable of IE Ver<9.
I changed the R_BROWSER env option to Google Chrome (which is my default browser). But getURL still uses IE as on execution of the above getURL command still redirects to browsers page. I upgraded the IE version to 11. But looks like viralheatAPI doesn't work on IE at all as it still getting redirected to browsers page.
Anybody faced this issue?
回答1:
Requests made by RCurl do not use your browser. They should use RCurl's own library for making HTTP requests.
Most likely the website checks the useragent string of the request to see it something is a valid browser. RCurl does not seem to specify a useragent string by default like your browser does. If you want to impersonate Chrome, go to http://www.whatsmyuseragent.com/ to see what your current useragent string is. Copy that value to a variable in R, then run
ua <- "<your full user agent here>"
getURL("https://www.viralheat.com/api/sentiment/review.json?text=i%20am%20happy&api_key=",
ssl.verifypeer=FALSE, .opts=list(useragent=ua))
and that should avoid the browser sniffing step to at least get you to the API.
来源:https://stackoverflow.com/questions/24418691/viralheat-api-issues-with-geturl-command-in-rcurl-package