使用请求时MissingSchema异常

时间:2019-07-18 10:31:29

标签: python python-requests

我有一个人员类别,该人员类别如下所示:

Datatracker.py类

@dataclass
class Person:
    resource_uri: str
    id: int
    name: str
    name_from_draft: str
    ascii: str
    ascii_short: Optional[str]
    user: str
    time: str
    photo: str
    photo_thumb: str
    biography: str
    consent: bool

现在,我定义了一个函数,该函数将充当生成器并根据时间戳在数据跟踪器中打印人员详细信息:

def people(self, since="1970-01-01T00:00:00", until="2038-01-19T03:14:07", name_contains=None):
       """
       A generator that returns people recorded in the datatracker. As of April
       2018, there are approximately 21500 people recorded.
       Parameters:
           since         -- Only return people with timestamp after this
           until         -- Only return people with timestamp before this
           name_contains -- Only return peopls whose name containing this string
       Returns:
           An iterator, where each element is as returned by the person() method
       """
       url = self.base_url + "/api/v1/person/person/?time__gt=" + since + "&time__lt=" + until
       if name_contains is not None:
           url = url + "&name__contains=" + name_contains
       while url is not None:
           r = self.session.get(url, verify=True)
           meta = r.json()['meta']
           objs = r.json()['objects']
           url = meta['next']
           for obj in objs:
               yield obj

当我尝试仅打印另一个文件中的人的姓名时,尽管它会打印所有姓名,但会出现如下所示的异常错误。 ** app.py **

import requests
import datatracker
import rfcindex

user=datatracker.DataTracker()
user_people = user.people("1970-01-01T00:00:00", "2038-01-19T03:14:07", "")
 for u in user_people:
    print(u['name'])
Error: raise MissingSchema(error)
requests.exceptions.MissingSchema: Invalid URL '/api/v1/person/person/?limit=20&name__contains=&time__gt=2010-01-01&time__lt=2020-01-01&offset=20': No schema supplied. Perhaps you meant http:///api/v1/person/person/?limit=20&name__contains=&time__gt=2010-01-01&time__lt=2020-01-01&offset=20?

请帮助

1 个答案:

答案 0 :(得分:0)

以下简单脚本重复了您的问题:

import requests
result = requests.get('/api/v1')

输出

Traceback (most recent call last):
  File "example.py", line 3, in <module>
    result = requests.get('/api/v1')
...
Several lines removed for brevity
...
requests.exceptions.MissingSchema: Invalid URL '/api/v1': No schema supplied. Perhaps you meant http:///api/v1?

requests库正试图告诉您在self.base_url前面加上“ http://”前缀,如

self.base_url = 'http://' + hostname

对于您的特定情况,您只需要在代码中添加一行即可:

import requests
import datatracker
import rfcindex

user = datatracker.DataTracker()
user.base_url = 'https://' + user.base_url
user_people = user.people("1970-01-01T00:00:00", "2038-01-19T03:14:07", "")
    for u in user_people:
        print(u['name'])

此完整示例显示了如何使用与您所使用的类存在相同问题的类从网站中获取前20个名称:

import requests

class DataTracker:
    def __init__(self):
        self.base_url = 'datatracker.ietf.org'

    def people(self):
        url = self.base_url + '/api/v1/person/person'
        result = requests.get(url)
        data = result.json()
        for value in data['objects']:
            yield value['name']

data_tracker = DataTracker()
data_tracker.base_url = 'https://' + data_tracker.base_url
for person in data_tracker.people():
    print(person)

如果无法使该示例程序正常工作,那么您还有其他问题。