使用BeautifulSoup发送POST请求时发生ViewExpiredException

时间:2020-05-06 00:04:15

标签: python post web-scraping beautifulsoup python-requests

发送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())




    ```


0 个答案:

没有答案