【问题标题】:Error importing data using requests - python使用请求导入数据时出错 - python
【发布时间】:2022-01-18 08:23:06
【问题描述】:

我正在尝试从以下网页访问数据:https://qships.tmr.qld.gov.au/webx/# 使用“船舶移动”选项卡。 我尝试使用 url 服务请求,但不断收到错误。

目前,我已经尝试过:

import requests
import pandas as pd
import json

url = 'https://qships.tmr.qld.gov.au/webx/services/wxdata.svc/GetDataX'

payload = {
"token": None,
"reportCode": "MSQ-WEB-0001",
"dataSource": None,
"filterName": "Last 7 days",
"parameters": [{
        "__type": "ParameterValueDTO:#WebX.Core.DTO",
        "sName": "DOMAIN_ID",
        "iValueType": 0,
        "aoValues": [{"Value": -1}],
              }],
"metaVersion": 0,
}

jsonData = requests.post(url, data = payload).json()

返回以下错误:

{'ExceptionDetail': {'HelpLink': None, 'InnerException': None, 'Message': "The incoming message has an unexpected message format 'Raw'. The expected message formats for the operation are 'Xml', 'Json'. This can be because a WebContentTypeMapper has not been configured on the binding. See the documentation of WebContentTypeMapper for more details.", 'StackTrace': '   at System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.DeserializeRequest(Message message, Object[] parameters)\r\n   at System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(Message message, Object[] parameters)\r\n   at System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc& rpc)\r\n   at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)\r\n   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc)\r\n   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc& rpc)\r\n   at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)', 'Type': 'System.InvalidOperationException'}, 'ExceptionType': 'System.InvalidOperationException', 'Message': "The incoming message has an unexpected message format 'Raw'. The expected message formats for the operation are 'Xml', 'Json'. This can be because a WebContentTypeMapper has not been configured on the binding. See the documentation of WebContentTypeMapper for more details.", 'StackTrace': '   at System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.DeserializeRequest(Message message, Object[] parameters)\r\n   at System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(Message message, Object[] parameters)\r\n   at System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc& rpc)\r\n   at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)\r\n   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc)\r\n   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc& rpc)\r\n   at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)'}

当我更改 json 字典时,我收到以下错误:

jsonData = requests.post(url, json={'key':'value'}).json()

{'ExceptionDetail': None, 'ExceptionType': None, 'Message': 'WebX.Security.EAuthError: Request does not belong to an authenticated session.', 'StackTrace': None}

数据可以通过请求访问还是我必须抓取它?

【问题讨论】:

  • "我想我需要一个更灵活的解析器" 那么你有没有尝试在你的自己发帖前按照How to Ask在这里发帖?您能以minimal reproducible example 的身份在这里分享您的尝试吗?
  • 我已将问题更新为更加细致入微。我不知道是导致崩溃的实际数据结构还是它不断刷新的事实。我尝试了多种方法。很高兴将它们全部列出以显示当前的工作。可能会因为问题不清楚而被否决。
  • 您仍然在单个页面中获取数据,但页面是使用 JavaScript 更新的,因此这个问题的答案与 SO 上 90% 的 bs4 问题的答案相同:涉及到 JavaScript,你需要使用解析和运行 JavaScript 的引擎,试试selenium。最重要的是:您可以查看网页下方的实际代码 - 您很可能可以直接发出 JS 自己发出的 Web 服务请求,而完全跳过抓取。
  • 谢谢。这是我所寻求的建议。我搜索了更多包含“更新”或“刷新”数据的问题,但找不到任何东西。我觉得这个问题不太高兴删除或重新配置,所以它提供了一些关于从哪里开始的高级信息。
  • 这绝对不是“同样的错误”。那是一个不同的错误。一种是说你的内容类型不正确(data= 将发送application/octet-streamjson= 将发送application/json,这里更正确),另一种是说你没有经过身份验证。

标签: python html beautifulsoup


【解决方案1】:

按照链接的 Web UI 发出的请求,此脚本似乎能够访问 一些 数据。

数据查询的 JSON 负载与原始帖子不同,因此可能需要在此处进行一些调整。

无论如何,关键在于您需要使用requests.Session(),并首先向 Web UI 发出请求以获取会话 cookie。

from pprint import pprint

import requests

get_session_url = "https://qships.tmr.qld.gov.au/webx/"
get_data_url = "https://qships.tmr.qld.gov.au/webx/services/wxdata.svc/GetDataX"
get_data_query = {
    "token": None,
    "reportCode": "MSQ-WEB-0001",
    "dataSource": None,
    "filterName": "Next 7 days",
    "parameters": [
        {
            "__type": "ParameterValueDTO:#WebX.Core.DTO",
            "sName": "DOMAIN_ID",
            "iValueType": 0,
            "aoValues": [{"Value": -1}],
        }
    ],
    "metaVersion": 0,
}

sess = requests.session()

sess.get(get_session_url).raise_for_status()

json_data = sess.post(
    get_data_url,
    json=get_data_query,
).json()

pprint(json_data)

打印出来(例如)

{'d': {'BuildVersion': '7.0.0.12590',
       'ReportCode': 'MSQ-WEB-0001',
       'Tables': [{'AsOfDate': '16:33 on Jan 17',
                   'BuildVersion': '7.0.0.12590',
                   'Data': [[132058,
                             334359,
                             'EXT',
                             'STOLT MOMIJI',
                             'TANKER',
                             121.52,
...

【讨论】:

  • 感谢 AKX。我会整理一下问题
  • 当你说一些数据时,你的意思是不是所有数据。或者用途有限。我正在尝试normalise 数据并返回一个 pandas df,以便它复制网页上显示的表格结构
  • 不知道是不是你需要的正确数据。
  • 好点。有表格结构对应Data
猜你喜欢
  • 2018-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-09
  • 2018-04-22
  • 1970-01-01
  • 2017-12-31
  • 1970-01-01
相关资源
最近更新 更多