想要爬取我们系统内网页的数据。如图所示,已经用chrome锁定了爬取内容–response中的json字符串.
首先,先通过保存cookie文件,读取token的方式模拟登陆网站,这部分没问题:
$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
Get-Content .\cookienyf.txt | foreach {
$line=$_ -split '/' | select -First 1
$tokens=$_.Split("`t").TrimEnd()
$c=@{
name=$tokens[0]
value=$tokens[1]
domain=$tokens[2]
}
$cookie = New-Object System.Net.Cookie
$cookie.Name=$c.name
$cookie.Value=$c.Value
$cookie.Domain=$c.domain
$session.Cookies.Add($cookie)
}
然后使用Invoke-RestMethod,试图抓取截图中‘response’里的json字符串
$response = Invoke-RestMethod $url -Method post -ContentType "application/json" -WebSession $session -OutFile output.json -UserAgent 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36'
结果报错。。。。["编码: JUSTEP150013; 提示: 解析Action出错","A JSONObject text must begin with '{' at character 0"]</messages></root>
求大神指点一二!