【发布时间】:2018-12-23 18:36:31
【问题描述】:
有没有办法将已经从 Powershell 中现有 Chrome 网络会话中获取的 cookie 用于 SessionVariable?
我知道 SessionVariable 的字段是:
Headers : {}
Cookies : System.Net.CookieContainer
UseDefaultCredentials : False
Credentials :
Certificates :
UserAgent : Mozilla/5.0 (Windows NT; Windows NT 10.0; en-US)
WindowsPowerShell/5.1.17134.407
Proxy :
MaximumRedirection : -1
我是否可以做一些事情来为网络请求设置这些值,例如:?当会话可能是多个/不同类型的请求时,我是否需要特定的标头?
$ua = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36"
$Params = @{
Headers = $headers
Cookies = $cookies
UseDefaultCredentials = "False"
Credentials = $creds
UserAgent = $ua
Proxy = ""
MaximumRedirection = "-1"
}
如果可以做到这一点,我也有点困惑如何输入 cookie。我是从Getting Cookies using PowerShell 找到的:
$webrequest = Invoke-WebRequest -Uri $url -SessionVariable websession
$cookies = $websession.Cookies.GetCookies($url)
# Here, you can output all of $cookies, or you can go through them one by one.
foreach ($cookie in $cookies) {
# You can get cookie specifics, or just use $cookie
# This gets each cookie's name and value
Write-Host "$($cookie.name) = $($cookie.value)"
}
我可以从任何给定站点以这种格式导出 cookie 的 JSON 格式,但如果需要的话,我也可以将其缩减为 "name" = "value"。
[
{
"domain": "",
"expirationDate": "",
"hostOnly": "",
"httpOnly": "",
"name": "",
"path": "",
"sameSite": "",
"secure": "",
"session": "",
"storeId": "",
"value": "",
"id": ""
},
{
"domain": "",
etc...
}
]
我不知道如何在 $cookies 中将每个部分指定为 $cookie,也不知道如何以不同的方式添加它们,因为它不是来自 URL/WebRequest 而是来自 JSON。
感谢您的帮助!
【问题讨论】:
-
每个浏览器都有自己的 cookie 存储方式。我刚刚了解到,Chrome 将 cookie 存储在您配置文件 (stackoverflow.com/questions/31021764/…) 中的 SQLite db 文件中,因此无论如何您都必须从那里提取 cookie 信息以在脚本中使用。如果可能的话……
标签: powershell web cookies session-cookies scrape