问题描述
如果我们在 Chrome 中访问 this url,并打开 devtools,我们可以清楚地看到一个 cookie 出现(在 chrome 开发者工具 -> 'application' -> 'cookies' 中).
If we visit this url in chrome, with devtools open, we can clearly see a cookie appear (in chrome developer tools -> 'application' -> 'cookies').
如果我们使用 httr::GET()
尝试同样的事情,我们希望看到 cookie,但我们没有:
If we attempt the same thing using httr::GET()
, we expect to see the cookie, but we do not:
library(httr)
r <- GET("https://aps.dac.gov.in/LUS/Public/Reports.aspx")
r$cookies
# [1] domain flag path secure expiration name value
# <0 rows> (or 0-length row.names)
这是为什么,我们如何最好使用 httr
和/或 rvest
(加上其他建议,但 无需使用实际浏览器,无头或其他方式,包括硒)
Why is this, and how can we retrieve the cookie (along with the page html) preferably using either httr
and/or rvest
(plus other suggestions but without using an actual browser, headless or otherwise, including selenium)
推荐答案
发生这种情况的原因是因为直到用户提交表单(通过打开 Chrome 开发人员工具并查看应用程序"-> 'cookies' 表单提交前后,我们看到cookie出现.
The reason this is happening is because the cookie doesn't actually get generated until the user submits the form (by opening chrome developer tools and watching 'application' -> 'cookies' before and after form submission, we see the cookie appear.
请注意,这可以使用 chrome incognito 进行模拟(它无法访问常规 chrome 中的 cookie,因此可以反复尝试以进行演示).
Note this can be emulated using chrome incognito (it won't have access to the cookies in regular chrome, so it can be tried repeatedly for demonstration purposes).
这篇关于无法获取 cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!