обрабатывать нумерацию страниц рекурсивно - PullRequest
1 голос
/ 08 мая 2019

Я использую requests lib для извлечения данных с удаленного сервера и сохраняю данные в модели, но мне нужно обработать нумерацию страниц, в настоящее время я загружаю только одну страницу с сервера.

У меня есть URL для нумерации страниц, например:

{
"status": "success",
"count": 32,
"total": 32,
"next": "https://pimber.ly/api/v2/products?sinceId=5c3ca8470985af0016229b5b",
"previous": "https://pimber.ly/api/v2/products?maxId=5c3ca8470985af0016229b04",
"sinceId": "5c3ca8470985af0016229b04",
"maxId": "5c3ca8470985af0016229b5b",
"data": [
    {
        "Primary ID": "API_DOCS_PROD1",
        "Product Name": "Example Product 1",
        "Product Reference": "Example Reference 1",
        "Buyer": "Example Buyer 1",
        "_id": "5c3ca8470985af0016229b04",
        "primaryId": "API_DOCS_PROD1"
    },

Я пытался использовать генератор Python для обработки текущей ситуации, но ничего не делает

_plimber_data = response.json()
yield _plimber_data
_next = _plimber_data['next']
print(_next)
for page in _next:
    _next_page = session.get(_plimber_data, params={'next': page}).json()
    yield _next_page['next']

    for _data in page:
        Product.objects.create(
            qr_id=_data['primaryId'],
            ean_code=_data['EAN'],
            description=_data['Description105'],
            category=_data['Category'],
            marketing_text=_data['Marketing Text'],
            bullet=_data['Bullet 1'],
            brand_image=_data['Brand Image'],
            image=_data['Images']
        )
        logger.debug(f'Something went wrong {_data}')
        print(f'This is the Data:{_data}')

Может кто-нибудь, пожалуйстаобъясните мне, как с этим справиться, чтобы я мог загрузить все данные в базу данных, спасибо.

1 Ответ

0 голосов
/ 08 мая 2019

Хорошо, так что я решил, двое думают, что первая функция генератора

def _get_product():
    """
    TODO: Fetch data from server
    """
    headers = {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
        'Authorization': settings.TOKEN
    }

    try:
        response = requests.get(
            url=f'{settings.API_DOMAIN}',
            headers=headers
        )
        response.raise_for_status()
    except HTTPError as http_err:
        print(f'HTTP error occurred: {http_err}')

    else:
        _plimber_data = response.json()
        while _plimber_data['next'] is not None:
            response = requests.get(
                _plimber_data['next'],
                headers=headers
            )
            _plimber_data = response.json()
            for _data in _plimber_data['data']:
                yield _data

Затем я перебираю функцию генератора и сохраняю данные:

    def run(self):
    _page_data = _get_product()
    for _product in _page_data:
        Product.objects.create(
            qr_id=_product['primaryId'],
            ean_code=_product['EAN'],
            description=_product['Description105'],
            category=_product['Category'],
            marketing_text=_product['Marketing Text'],
            bullet=_product['Bullet 1'],
            brand_image='\n'.join(_product['Brand Image']),
            image='\n'.join(_product['Images'])
        )
        logger.debug(f'Something went wrong {_product}')
        print(f'This is the Data:{_product}')
...