InvalidSchema(“没有为'%s'找到连接适配器'”%url) - 尝试了很多

时间:2018-06-15 14:05:54

标签: python url

我知道这个话题已经存在,但我无法弄清问题是什么。

这是代码(我相信它是直截了当的)

import requests
import config
import time

dataID = "111111"

# If reportis not generated will return 'None'
def get_data():
    dataChecking = None
    checking_url = 'https://api.example.com/v1.1/reports/{0}'.format(dataID)
    responseCheck = requests.get(checking_url, params=(('fields',
                                 'generated_date'), ), auth=(''
                                 + config.authUsername + '', ''
                                 + config.authPassword + ''))
    report_url = responseCheck.json()['report']['generated_date']
    dataChecking = requests.get(report_url).content
    return dataChecking


def download_report(dataChecking):
    urlDownload = 'https://api.example.com/v1.1/reports/{0}'.format(dataID)
    responseDownload = requests.get(urlDownload, params=(('fields',
                                    'download'), ), auth=(''
                                    + config.authUsername + '', ''
                                    + config.authPassword + ''))
    report_url = responseDownload.json()['report']['download']
    dataDownload = requests.get(report_url).content
    with open('' + config.fileDest + '\exportReport.json', 'w') as f:
        f.write(dataDownload)
    pass


# Checking report is generated
generatedData = get_data()

# Wating for report to generated
while generatedData == None:

    # Check again if report is generated
    print("Report is generating, Please wait")
    generatedData = get_data()

    # Wait 0.25 seconds between each check
    time.sleep(0.25)

# Report generated, need to download him
download_report(dataChecking)

错误是:

  raise InvalidSchema("No connection adapters were found for '%s'" % url)
InvalidSchema: No connection adapters were found for '2018-06-15T10:37:50'

我尝试使用不同的教程更改URL部分,但没有成功。

'2018-06-15T10:37:50' - 这是生成报告的日期,所以我目前所做的是尝试检查数据是否为空并继续检查它是否已填写(日期,如我在例子)然后它将运行下载部分。

1 个答案:

答案 0 :(得分:0)

您的错误似乎出现在get_data函数的这些行中:

report_url = responseCheck.json()['report']['generated_date']
dataChecking = requests.get(report_url).content

数据结构意味着您在变量report_url中存储的内容实际上是一个日期,然后您尝试将其作为url检索,从而引发错误。找出存储实际报告网址的位置,然后获取它。