0

我正在尝试访问一个根据访问者类型设置 cookie 的网站:

library(httr)
url <- "https://www.blackrock.com/ca/individual/en/products/product-list#categoryId=1&lvl2=overview"

基本上我尝试过的所有东西都会返回这个......:

GET(url)

Response [https://www.blackrock.com/ca/individual/en/site-entry?targetUrl=%2Fca%2Findividual%2Fen%2Fproducts%2Fproduct-list%3FsiteEntryPassthrough%3Dtrue]
  Status: 200
  Content-type: text/html;charset=UTF-8
  Size: 270 kB
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml" prefix="og: http://ogp.me/ns#" lang="en" xml:lang="en"><head><meta content="text/html;charset=UTF-8" http-equiv="Content-type...
<meta name="description" content="" />
<meta name="robots" content="noindex,noarchive,nocache" />
<meta property="og:title" content="Site Entry" />
<meta property="og:type" content="website" />
<meta property="og:image" content="/amer-retail-assets/include/common/images/blackrock_logo.png" />
<meta property="og:site_name" content="BlackRock" />
<meta property="og:locale" content="en_CA" />
<meta property="og:url" content="https://www.blackrock.com/ca/individual/en/site-entry?locale=en_CA" />
<link rel="canonical" href="https://www.blackrock.com/ca/individual/en/site-entry?locale=en_CA" />

因此,我尝试在 chrome 中手动传递网关或站点入口页面后复制 cookie:

cookie = c(`JSESSION_amer-retail01` = "AC91C17F269D8F6A136E576531FA6E05",
`UnicaID` = "10.39.18.249-1408392737241856",
`UnicaNIODID` = "NQHKNKQYKDm-YxUEYgX",
`_ga` = "GA1.2.127078965.1408392737",
`blkUserType-ca-one` = "institutional",
`s_pers` = "%20s_fid%3D14569E53C0F4EE51-3982590542B2DDA4%7C1471566685393%3B%20gpv%3Dca-one%257Cproducts%257Cproduct%2520list%7C1408410085400%3B%20s_nr%3D1408408285405-Repeat%7C1439944285405%3B%20s_pers_eVar15%3Dprospect%7C1411000285408%3B%20s_pers_prop19%3Danonymous%7C1411000285411%3B",
`s_sess` = "%20s_cc%3Dtrue%3B%20s_sq%3D%3B", 
`s_vi` = "[CS]v1|29F92F108507B6C6-6000010A8000E2ED[CE]",
`ts-ca-one-locale` = "en_CA")

GET(url, set_cookies(.cookies = cookie), user_agent("Mozilla/5.0"))

这将返回相同的结果。

但是,当我查看示例时,httr我认为设置 cookie 得到的结果不正确。

例如:

> example(set_cookies)

st_cks> set_cookies(a = 1, b = 2)
Config: 
List of 1
 $ cookie:"a1=;b2="

st_cks> set_cookies(.cookies = c(a = "1", b = "2"))
Config: 
List of 1
 $ cookie:"a1=;b2="

st_cks> GET("http://httpbin.org/cookies")
Response [http://httpbin.org/cookies]
  Status: 200
  Content-type: application/json
  Size: 19 B
{
  "cookies": {}

st_cks> GET("http://httpbin.org/cookies", set_cookies(a = 1, b = 2))
Response [http://httpbin.org/cookies]
  Status: 200
  Content-type: application/json
  Size: 50 B
{
  "cookies": {
    "a1": "", 
    "b2": ""
  }

我本来希望 httpbin.org 返回 a = 1 和 b = 1 而不是 a1 = 和 b2 =。难道我做错了什么?

4

1 回答 1

2

你没有做错任何事(至少在 IMO)。看一下set_cookies,那里有一些代码:

cookie <- paste0(names(cookies), cookies_str, sep = "=", collapse = ";")

这没有按预期工作(即它a = "1"变成a1=而不是a=1)。

你可以使用这个:

sc2 <- function (..., .cookies = character(0)) {

  cookies <- c(..., .cookies)
  stopifnot(is.character(cookies))
  cookies_str <- vapply(cookies, RCurl::curlEscape, FUN.VALUE = character(1))
  cookie <- paste(names(cookies), cookies_str, sep = "=", 
                   collapse = ";")
  config(cookie = cookie)

}

GET("http://httpbin.org/cookies", sc2(a = 1, b = 2))

## Response [http://httpbin.org/cookies]
##   Status: 200
##   Content-type: application/json
##   Size: 50 B
## {
##   "cookies": {
##     "a": "1", 
##     "b": "2"
##   }

在修复之前使用paste而不是paste0暂时使用(我将在httrgithub repo 上提出问题)。

于 2014-08-19T10:50:09.333 回答