1

我需要帮助,尝试使用 Python 对过去 24 小时内更新的票证发出以下请求,但根据提供的示例似乎无法弄清楚这一点。它在 Postman 中给了我一个有效的响应,所以 GET 请求有效,我只是无法弄清楚如何编码。

我试图做的查询是:https://clubready.zendesk.com/api/v2/search.json?sort_by=updated_at&sort_order=desc&query=updated>24hours type:ticket

我的代码是:

import requests

url = "https://clubready.zendesk.com/api/v2/search.json"

querystring = {"sort_by":"updated_at","sort_order":"desc","query":"updated%3E24hours%20type:ticket"}

payload = ""
headers = {
    'Authorization': "Basic ZHJvYmluc29uQGNsdWJyZWFkeS5jb206QWNlbWFuMTAh",
    'User-Agent': "PostmanRuntime/7.15.0",
    'Accept': "*/*",
    'Cache-Control': "no-cache",
    'Postman-Token': "9d56b0e2-729f-4170-88f3-bcf9dbfa1020,803b5113-075b-4838-9fad-0d819c389c7a",
    'Host': "clubready.zendesk.com",
    'cookie': "__cfduid=d0076a9c776e07ff900489e935bb86e691558998542; __cfruid=03f547455e192905682b701e2bc5521a99d71c6c-1560442970; _zendesk_shared_session=-NEV5WTdPS1kyb1Z4SHBDbnk1d1dsWWhqUGloTTg1WEQybGs4L3FkNGRMakZtN0g3aGlTRlV1S3RrV1dlTllWWVFySUZvNkFFWHM5VVlEUDVyM0g1OFV3Q3grZjNIYWVwZGR1MlA4T05NN2NzZDlxMTZqUm0rNk9pVzFOSjk4M0I5SnB2d056MVNiZ28rWEdhMS9uQmhWT3g0MG1iaEtmVUpreTJzOXRRbVZHYUEvN1AyVk1KWkRtSzdiOVQvV2tkU0hia2VhenRCRjhGY3k1TTRFQ2dzUT09LS1BT2FIWEhzUXVwWGNxZ3lBcXBReklRPT0%3D--3c394a5133686509e48321137b451c2d037cf3ee; _help_center_session=NndtdTNYTDl3NTRWT1lLVGlLT1pYMVI4NVF5MncxOTVYdTlZWG1QTlJ5cGtTbXdPeGYyL2R6TWVwdXZXeUV6U2p3NlR0eHdnR29GOFlSMVpTalJvMUFRNCs1SkJqZHdJQXpOQzNveCtyT2xFU2pycGtUdEZRYWRnWHh1Z1YzcjFVWk5Ec1VoWjV0U2JXZGJTMURkT0djdnRiL3F6VlN2QU1IWDNSSVJRUTNuSm1mK21XZDdtSGhGaDc4bStsQzdIZ3dQeVVESW1sV3VaOGJuSjBjSjlBYWJ3a3J3bzEraHVDelJBclFtbk10RT0tLWMyTWxDNHpNQWcvVFRZbXo3R0szZkE9PQ%3D%3D--047ca57c1f10d81ce14f8de917a3ff6f43e9ef7b; _zendesk_session=BAh7C0kiD3Nlc3Npb25faWQGOgZFVEkiJWM3NjFiY2E5Zjc3YWVmMDg3Mjc2M2ZlNzk3MzYxZTFjBjsAVEkiDGFjY291bnQGOwBGaQKIAUkiCnJvdXRlBjsARmkDKzoBSSIOaXNfbW9iaWxlBjsAVEZJIhN3YXJkZW4ubWVzc2FnZQY7AFR7AEkiEF9jc3JmX3Rva2VuBjsARkkiRVlESmVEL1NWYVNhKzZ2YjRIRENaUGlBa29mdWtUZlArVU5QNEU3RXpTcjdhMlYwNG5OdzFVMU5INk1yY1pCTVoGOwBG--a237bcedbc9d391a74d2ad7b549bc7835b0e7566",
    'accept-encoding': "gzip, deflate",
    'Connection': "keep-alive",
    'cache-control': "no-cache"
    }

response = requests.request("GET", url, data=payload, headers=headers, params=querystring)

print(response.text)

{"results":[],"facets":null,"next_page":null,"previous_page":null,"count":0}

我在使用代码时得到了这个,但我在 Postman 中得到了一个实际的列表。

4

1 回答 1

1

这与我通常在 Python 中获取 Zendesk 票证数据的查询非常相似。

首先,让我们导入依赖项:

import requests
from urllib.parse import urlencode

现在,让我们创建查询字符串并将其添加到包含所有请求参数的字典中:

queryString = "updated>24hours type:ticket"

params = {
    'query': queryString,
    'sort_by': 'updated_at',
    'sort_order': 'desc'
}

我们需要做的最后一件事是指定,url你需要:userpwd

url = 'https://clubready.zendesk.com/api/v2/search.json?' + urlencode(params)
user = YOUR_EMAIL + '/token'
pwd = YOUR_TOKEN

现在我们准备好提出请求了。为此,请设置一个while循环来读取响应.json并将其附加到resultsList.

#use a counter to print the current page number
counter = 0

while url:
    response = requests.get(url, auth=(user, pwd))
    if response.status_code == 429:
        #handle rate limiting with Zendesk's recommended timeout value
        print('Rate limited! Please wait.')
        time.sleep(int(response.headers['retry-after']))
        continue
    if response.status_code != 200:
        #for any other code, exist the request
        print('Status:', response.status_code, 'Problem with the request. Exiting.')
        exit()

    #if the request went through, read through the json and add each ticket to resultsList
    data = response.json()

    for result in data['results']:
        resultsList.append(result)

    #print the current page number. This is not necessary, but nice
    counter += 1
    print(str(counter) + ", ", end='')

    #get the url of the next page that is given inside the results json
    url = data['next_page']

    #timeout for 1 second to avoid accidental rate limiting
    time.sleep(1)

在阅读每一页后使用超时帮助我避免了奇怪的限速事件。

resultsList将是一个列表,每个项目代表一张票。因此列表的长度与您的查询找到的票数相同。此列表可用于创建 Pandas DataFrame,您可以在其中进一步分析您的票证数据。

于 2019-07-19T09:29:02.570 回答