使用BeautifulSoup发送POST请求时的ViewExpiredException

2024-09-29 23:27:07 发布

您现在位置:Python中文网/ 问答频道 /正文

发送POST请求通常会出现以下错误:

<partial-response id="j_id1">
 <error>
  <error-name>
   class javax.faces.application.ViewExpiredException
  </error-name>
  <error-message>
   <![CDATA[viewId:/user/searchstatus.xhtml - View /user/searchstatus.xhtml could not be restored.]]>
  </error-message>
 </error>
</partial-response>

我正在做的是,这个网站使用验证码来获取车辆信息。所以我首先要解决验证码,然后获取数据。以上错误大部分时间都会显示出来。只有几次我得到的结果,有时验证码不匹配发生。 如果缓存导致问题,则尝试缓存控制为无缓存。但仍然相同。 这是密码





app_url = 'https://vahan.nic.in/nrservices/faces/user/searchstatus.xhtml'





button = soup.find("button",{"type": "submit"})


encodedViewState = viewstate.replace("/", "%2F").replace("+", "%2B").replace("=", "%3D")


headers = {
    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
    'Accept': 'application/xml, text/xml, */*; q=0.01',
    'Accept-Language': 'en-us',
    'Accept-Encoding': 'gzip, deflate, br',
    'Host': 'vahan.nic.in',
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0 Safari/605.1.15',
    'Cookie': 'JSESSIONID=%s; SERVERID_7081=vahanapi_7081; SERVERID_7082=nrservice_7082' % cookies['JSESSIONID'],
    'X-Requested-With':'XMLHttpRequest',
    'Faces-Request':'partial/ajax',
    'Origin':'https://vahan.nic.in',
    'Referer':'https://vahan.nic.in/nrservices/faces/user/searchstatus.xhtml',
    'Connection':'keep-alive'

}

viewstate = soup.select('input[name="javax.faces.ViewState"]')[0]['value']
data = {
    'javax.faces.partial.ajax':'true',
    'javax.faces.source': button['id'],
    'javax.faces.partial.execute':'@all',
    'javax.faces.partial.render': 'rcDetailsPanel resultPanel userMessages capatcha txt_ALPHA_NUMERIC',
    button['id']:button['id'],
    'masterLayout':'masterLayout',
    'regn_no1_exact': 'pb35n4655',
    'txt_ALPHA_NUMERIC': ans,
    'javax.faces.ViewState': viewstate,
    'j_idt42':''
}


postResponse = requests.post(url=app_url, data=data,headers=headers, cookies=cookies )


rsoup = BeautifulSoup(postResponse.text, 'html.parser')

print(rsoup.prettify())




    ```



Tags: nameinidapplicationbuttonerrorpartial验证码

热门问题