【发布时间】: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-stream,json=将发送application/json,这里更正确),另一种是说你没有经过身份验证。
标签: python html beautifulsoup