python 请求后查询失败 :cookies?

标签 python post web-scraping python-requests

我正在尝试执行此post 但我收到服务器错误 500:

import requests
base_url = "https://www.assurland.com/ws/CarVehiculeSearch.asmx"
url = "%s/%s"% (base_url,"GetCarBodyTypeListByCarAlim")
pars ={"CarAlim":"DIES","CarType": "A7", "CodeMake": "AUDI", "FirstDrivingDate": "2015-09-22"}

with requests.Session() as s:
    r = s.post(url,data=pars)
    print r.status_code

 ## 500 

我想我需要设置 cookie 或其他东西。

提前感谢您的帮助。

最佳答案

该站点上的大多数 API 访问点似乎已损坏。您的代码本身没有任何问题。我实际上无法让网站响应他们自己的网络界面中的任何请求,即使使用常规浏览器也是如此。

main API documentation您还可以使用 SOAP protocol 的详细信息(法语)创建查询; Python 有 several SOAP client libraries您可以选择。

但是,我没有运气让它工作或者。使用优秀zeep library我试图访问更简单的 GetMainCarMakeListByFirstDrivingDate endpoint (请注意,我必须使用 datetime() 对象来模拟时间戳):

>>> from zeep import Client
>>> client = Client('https://www.assurland.com/ws/CarVehiculeSearch.asmx?WSDL')
>>> client.service.GetMainCarMakeListByFirstDrivingDate(FirstDrivingDate=datetime(2015, 9, 22))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/mjpieters/Development/venvs/stackoverflow-2.7/lib/python2.7/site-packages/zeep/client.py", line 25, in __call__
    self._op_name, args, kwargs)
  File "/Users/mjpieters/Development/venvs/stackoverflow-2.7/lib/python2.7/site-packages/zeep/wsdl/bindings/soap.py", line 109, in send
    return self.process_reply(client, operation_obj, response)
  File "/Users/mjpieters/Development/venvs/stackoverflow-2.7/lib/python2.7/site-packages/zeep/wsdl/bindings/soap.py", line 145, in process_reply
    return self.process_error(doc, operation)
  File "/Users/mjpieters/Development/venvs/stackoverflow-2.7/lib/python2.7/site-packages/zeep/wsdl/bindings/soap.py", line 223, in process_error
    detail=fault_node.find('detail'))
zeep.exceptions.Fault: <exception str() failed>

Fault 在 SOAP 中相当于 500 错误。

当我 enable debug logging ,我们可以看到服务器在生成响应时遇到问题:

>>> client.service.GetMainCarMakeListByFirstDrivingDate(FirstDrivingDate=datetime(2015, 9, 22))
zeep.transports: HTTP Post to https://www.assurland.com/ws/CarVehiculeSearch.asmx:
<?xml version='1.0' encoding='utf-8'?>
<soap-env:Envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
  <soap-env:Body>
    <ns0:GetMainCarMakeListByFirstDrivingDate xmlns:ns0="http://tempuri.org/">
      <ns0:FirstDrivingDate>2015-09-22T00:00:00</ns0:FirstDrivingDate>
    </ns0:GetMainCarMakeListByFirstDrivingDate>
  </soap-env:Body>
</soap-env:Envelope>

zeep.transports: HTTP Response from https://www.assurland.com/ws/CarVehiculeSearch.asmx (status: 500):
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><soap:Fault><faultcode>soap:Server</faultcode><faultstring>Le serveur n'a pas pu traiter la demande. ---&gt; Erreur lors de la génération du document XML. ---&gt; Le type common.FormDataListItem n'était pas attendu. Utilisez l'attribut XmlInclude ou SoapInclude pour spécifier les types qui ne sont pas connus statiquement.</faultstring><detail /></soap:Fault></soap:Body></soap:Envelope>
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/mjpieters/Development/venvs/stackoverflow-2.7/lib/python2.7/site-packages/zeep/client.py", line 25, in __call__
    self._op_name, args, kwargs)
  File "/Users/mjpieters/Development/venvs/stackoverflow-2.7/lib/python2.7/site-packages/zeep/wsdl/bindings/soap.py", line 109, in send
    return self.process_reply(client, operation_obj, response)
  File "/Users/mjpieters/Development/venvs/stackoverflow-2.7/lib/python2.7/site-packages/zeep/wsdl/bindings/soap.py", line 145, in process_reply
    return self.process_error(doc, operation)
  File "/Users/mjpieters/Development/venvs/stackoverflow-2.7/lib/python2.7/site-packages/zeep/wsdl/bindings/soap.py", line 223, in process_error
    detail=fault_node.find('detail'))
zeep.exceptions.Fault: <exception str() failed>

里面隐藏着错误信息:

Le serveur n'a pas pu traiter la demande. ---> Erreur lors de la génération du document XML. ---> Le type common.FormDataListItem n'était pas attendu. Utilisez l'attribut XmlInclude ou SoapInclude pour spécifier les types qui ne sont pas connus statiquement.

或者,在谷歌翻译的帮助下,用英语:

The server could not process the request. ---> Error generating the XML document. ---> The type common.FormDataListItem was not expected. Use XmlInclude or SoapInclude attribute to specify types that are not known statically.

由于我们没有发送任何 common.FormDataListItem 类型,并且消息提示无法生成 XML 文档,这看起来像对我来说是服务器端编程错误。

有些方法可以通过 SOAP 起作用:

>>> client.service.GetCarTypeListByCodeMake(CodeMake='BMW', FirstDrivingDate=datetime(2016, 1, 1))
['I3', 'I8', 'M2', 'M3', 'M4', 'M5', 'M6', 'SERIE 1 II', 'SERIE 2', 'SERIE 3 VI', 'SERIE 4', 'SERIE 5', 'SERIE 6', 'SERIE 7', 'X1', 'X3', 'X4', 'X5', 'X6', 'Z4']
>>> client.service.GetAllCarTypeListByCodeMake(CodeMake='BMW')
['1502', '1600', '1602', '1800', '1802', '2000', '2002', '2500', '3,0', '3,3', '315', '316', '318', '320', '323', '324', '325', '328', '330', '518', '520', '523', '524', '525', '528', '530', '535', '540', '545', '550', '628', '630', '633', '635', '645', '650', '725', '728', '730', '732', '733', '735', '740', '745', '750', '760', '840', '850', 'I3', 'I8', 'L7', 'M2', 'M3', 'M4', 'M5', 'M535', 'M6', 'M635', 'SERIE 1', 'SERIE 1 II', 'SERIE 2', 'SERIE 3', 'SERIE 3 (SUITE)', 'SERIE 3 VI', 'SERIE 4', 'SERIE 5', 'SERIE 6', 'SERIE 7', 'X1', 'X3', 'X4', 'X5', 'X6', 'Z1', 'Z3', 'Z4', 'Z8']

或通过使用requests 发布application/x-www-form-urlencoded 数据:

>>> response = requests.post('https://www.assurland.com/ws/CarVehiculeSearch.asmx/GetCarTypeListByCodeMake', data={'CodeMake': 'BMW', 'FirstDrivingDate': datetime(2016, 1, 1)})
>>> print(response.text)
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfAnyType xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
  <anyType xsi:type="xsd:string">I3</anyType>
  <anyType xsi:type="xsd:string">I8</anyType>
  <anyType xsi:type="xsd:string">M2</anyType>
  <anyType xsi:type="xsd:string">M3</anyType>
  <anyType xsi:type="xsd:string">M4</anyType>
  <anyType xsi:type="xsd:string">M5</anyType>
  <anyType xsi:type="xsd:string">M6</anyType>
  <anyType xsi:type="xsd:string">SERIE 1 II</anyType>
  <anyType xsi:type="xsd:string">SERIE 2</anyType>
  <anyType xsi:type="xsd:string">SERIE 3 VI</anyType>
  <anyType xsi:type="xsd:string">SERIE 4</anyType>
  <anyType xsi:type="xsd:string">SERIE 5</anyType>
  <anyType xsi:type="xsd:string">SERIE 6</anyType>
  <anyType xsi:type="xsd:string">SERIE 7</anyType>
  <anyType xsi:type="xsd:string">X1</anyType>
  <anyType xsi:type="xsd:string">X3</anyType>
  <anyType xsi:type="xsd:string">X4</anyType>
  <anyType xsi:type="xsd:string">X5</anyType>
  <anyType xsi:type="xsd:string">X6</anyType>
  <anyType xsi:type="xsd:string">Z4</anyType>
</ArrayOfAnyType>

您可能必须联系此 API 的维护者才能解决此问题,这不是您可以在 Python 端工作的事情。

关于python 请求后查询失败 :cookies?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40420578/

相关文章:

http - Racket 通过 http-client 或 post-impure-port 上传带有参数的 zip 文件

python - jitclass 与扩展 API : what can be used in a list?

Python - linalg.eigsh 如何找到*所有*特征向量?

python - 复制 AppEngine 查询对象以创建过滤器的变体,而不影响基本查询

ruby-on-rails - Rails Mechanize - 下载文件

python - 使用 BeautifulSoup 抓取亚马逊网页

node.js - 我如何优化在不同操作系统上运行的网页抓取脚本

python - 处理字典的字典

ios - obj-c AFNetworking 2.0 POST 请求不起作用

javascript - C - 使用 libcurl 的 http post 请求