python - Matomo 以前的 Piwik API.getBulkRequest 返回空列表

标签 python python-requests matomo

这真的很奇怪: API.getBulkRequest 不适用于 python 请求

如果我在浏览器中尝试同样的操作,效果很好。

如果我尝试在 python 中执行任何其他 matomo 请求,它工作正常。

这是我的代码:

对于网址,我只是使用了 Matomo 报告 API 引用中的示例。

import requests

urls = [
    'method%3dVisitsSummary.get%26idSite%3d1%26date%3d2012-03-06%26period%3dday',
    'method%3dVisitorInterest.getNumberOfVisitsPerVisitDuration%26idSite%3d1%26date%3d2012-03-06%26period%3dday']
params = {
    'method': 'API.getBulkRequest',
    'urls': urls,
    'format': 'json',
    'module': 'API',
    'token_auth': MATOMO_SECRECT_TOKEN,
}
r = requests.post(api_url, params)
print(r)
print(r.json())

Result:
<Response [200]>
[]

有人能看出明显的错误吗?

最佳答案

以下方法有效:

import requests

api_url="https://demo.matomo.org/"

params = {
    'method': 'API.getBulkRequest',
    'urls[0]': 'method%3dVisitsSummary.get%26idSite%3d3%26date%3d2012-03-06%26period%3dday',
    'urls[1]': 'method%3dVisitorInterest.getNumberOfVisitsPerVisitDuration%26idSite%3d3%26date%3d2012-03-06%26period%3dday',
    'format': 'json',
    'module': 'API',
    'token_auth': "anonymous",
}
r = requests.post(api_url, params)
print(r)
print(r.json())

&urls[0]=test&urls[1]=test 对于 PHP 来说有点特殊,请求不会自动生成这样的结构。

您可以添加这样的参数:

for index, url in enumerate(urls):
  params["urls[{count}]".format(count=index)] = url

关于python - Matomo 以前的 Piwik API.getBulkRequest 返回空列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48711996/

相关文章:

javascript - 使用 websocket 在服务器中反序列化 html 表单

Python、Mac OS X NSUserNotification、操作按钮

xpath - 使用 lxml 和 requests 从 XPath 返回 utf-8

php - 使用 php 安装 Piwik

javascript - 跟踪下载,包括自定义尺寸

python - 为什么表格在 Python-markdown 中不起作用?

用于随机数生成的 Python pycrypto 库与 os.urandom

Python 请求 : download only if newer

python - 我怎样才能让这个简单的Python登录程序循环起来?

google-app-engine - 如何为谷歌云应用程序版本添加可写权限?