Почему я получаю ответ 400 в запросах Python?

#python-3.x #post #web-scraping #python-requests #web-crawler

Вопрос:

Я хочу ползти https://www.ketto.org/crowdfunding/fundraisers. Я нашел один URL-адрес https://nn2uorrizx-dsn.algolia.net/1/indexes/*/запросы, из которых я могу получить данные из запроса post, но я получаю ответ 400 вместо 200. Пожалуйста, помогите мне очистить данные !

Вот мой код:

     import requests

    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:90.0) Gecko/20100101 Firefox/90.0',
        'Accept': 'application/json',
        'Accept-Language': 'en-US,en;q=0.5',
        'Content-Type': 'application/x-www-form-urlencoded',
        'Origin': 'https://www.ketto.org',
        'Sec-Fetch-Dest': 'empty',
        'Sec-Fetch-Mode': 'cors',
        'Sec-Fetch-Site': 'cross-site',
        'Referer': 'https://www.ketto.org/',
        'Connection': 'keep-alive',
    }

    params = (
        ('x-algolia-agent', 'Algolia for JavaScript (3.35.1); Browser (lite); angular (8.2.14); angular-instantsearch (3.0.0-beta.4); instantsearch.js (3.7.0); JS Helper (2.28.1)'),
        ('x-algolia-application-id', 'NN2UORRIZX'),
        ('x-algolia-api-key', 'b2caa1b0589e8db9398d5fe2a40bbaed'),
    )

    data = [
      ('{requests:[{indexName:fundraiser_prod,params:query', ''),
      ('hitsPerPage', '9'),
      ('hitsPerPage', '1'),
      ('hitsPerPage', '1'),
      ('hitsPerPage', '1'),
      ('maxValuesPerFacet', '10'),
      ('maxValuesPerFacet', '10'),
      ('maxValuesPerFacet', '10'),
      ('maxValuesPerFacet', '10'),
      ('page', '1'),
      ('page', '0'),
      ('page', '0'),
      ('page', '0'),
      ('highlightPreTag', '__ais-highlight__'),
      ('highlightPreTag', '__ais-highlight__'),
      ('highlightPreTag', '__ais-highlight__'),
      ('highlightPreTag', '__ais-highlight__'),
      ('highlightPostTag', '__/ais-highlight__'),
      ('highlightPostTag', '__/ais-highlight__'),
      ('highlightPostTag', '__/ais-highlight__'),
      ('highlightPostTag', '__/ais-highlight__'),
      ('facets', '["cause.label","tags","address"]'),
      ('facets', '["cause.label"]'),
      ('facets', '["tags"]'),
      ('facets', '["address"]'),
      ('tagFilters', ''),
      ('tagFilters', ''),
      ('tagFilters', ''),
      ('tagFilters', ''),
      ('facetFilters', '[["cause.label:"],["tags:"],["address:"]]},{indexName:fundraiser_prod,params:query='),
      ('facetFilters', '[["tags:"],["address:"]]},{indexName:fundraiser_prod,params:query='),
      ('facetFilters', '[["cause.label:"],["address:"]]},{indexName:fundraiser_prod,params:query='),
      ('facetFilters', '[["cause.label:"],["tags:"]]}]}'),
      ('attributesToRetrieve', '[]'),
      ('attributesToRetrieve', '[]'),
      ('attributesToRetrieve', '[]'),
      ('attributesToHighlight', '[]'),
      ('attributesToHighlight', '[]'),
      ('attributesToHighlight', '[]'),
      ('attributesToSnippet', '[]'),
      ('attributesToSnippet', '[]'),
      ('attributesToSnippet', '[]'),
      ('analytics', 'false'),
      ('analytics', 'false'),
      ('analytics', 'false'),
      ('clickAnalytics', 'false'),
      ('clickAnalytics', 'false'),
      ('clickAnalytics', 'false'),
    ]

    response = requests.post('https://nn2uorrizx-dsn.algolia.net/1/indexes/*/queries', headers=headers, params=params, data=data)

    print(response)
 

Если есть какие-либо другие предложения для обхода https://www.ketto.org/crowdfunding/fundraisers используя модуль запросов python, я бы с удовольствием сделал это здесь. Заранее спасибо.

Ответ №1:

Чтобы получить правильный ответ от сервера, используйте json= параметр в requests :

 import json
import requests

url = "https://nn2uorrizx-dsn.algolia.net/1/indexes/*/queries"

params = {
    "x-algolia-agent": "Algolia for JavaScript (3.35.1); Browser (lite); angular (8.2.14); angular-instantsearch (3.0.0-beta.4); instantsearch.js (3.7.0); JS Helper (2.28.1)",
    "x-algolia-application-id": "NN2UORRIZX",
    "x-algolia-api-key": "b2caa1b0589e8db9398d5fe2a40bbaed",
}

data = {
    "requests": [
        {
            "indexName": "fundraiser_prod",
            "params": "query=amp;hitsPerPage=9amp;maxValuesPerFacet=10amp;page=0amp;highlightPreTag=__ais-highlight__amp;highlightPostTag=__/ais-highlight__amp;facets=["cause.label","tags","address"]amp;tagFilters=amp;facetFilters=[["cause.label:"],["tags:"],["address:"]]",
        },
        {
            "indexName": "fundraiser_prod",
            "params": "query=amp;hitsPerPage=1amp;maxValuesPerFacet=10amp;page=0amp;highlightPreTag=__ais-highlight__amp;highlightPostTag=__/ais-highlight__amp;attributesToRetrieve=[]amp;attributesToHighlight=[]amp;attributesToSnippet=[]amp;tagFilters=amp;analytics=falseamp;clickAnalytics=falseamp;facets=["cause.label"]amp;facetFilters=[["tags:"],["address:"]]",
        },
        {
            "indexName": "fundraiser_prod",
            "params": "query=amp;hitsPerPage=1amp;maxValuesPerFacet=10amp;page=0amp;highlightPreTag=__ais-highlight__amp;highlightPostTag=__/ais-highlight__amp;attributesToRetrieve=[]amp;attributesToHighlight=[]amp;attributesToSnippet=[]amp;tagFilters=amp;analytics=falseamp;clickAnalytics=falseamp;facets=["tags"]amp;facetFilters=[["cause.label:"],["address:"]]",
        },
        {
            "indexName": "fundraiser_prod",
            "params": "query=amp;hitsPerPage=1amp;maxValuesPerFacet=10amp;page=0amp;highlightPreTag=__ais-highlight__amp;highlightPostTag=__/ais-highlight__amp;attributesToRetrieve=[]amp;attributesToHighlight=[]amp;attributesToSnippet=[]amp;tagFilters=amp;analytics=falseamp;clickAnalytics=falseamp;facets=["address"]amp;facetFilters=[["cause.label:"],["tags:"]]",
        },
    ]
}


data = requests.post(url, params=params, json=data).json()
print(json.dumps(data, indent=4))
 

С принтами:

 {
    "results": [
        {
            "hits": [
                {
                    "id": 180773,
                    "title": "Feeding From Far-Ration Distribution Amid Lockdown",
                    "end_date": "2021-09-11 23:59:59",
                    "amount_requested": 65000000,
                    "entity_details_id": 1505699,
                    "creator_entity_details_id": 1463388,
                    "address_1": "Mumbai",
                    "creation_date": "2020-04-05 15:10:12",
                    "parent_cause_id": 48,
                    "event_entity_details_id": 1399904,
                    "sucess_story_flag": 0,
                    "custom_tag": "FeedingFromFarForCorona",
                    "beneficiary": {
                        "full_name": "Prayatna "
                    },
                    "raised": {
                        "campaign_id": 180773,
                        "backers": 10616,
                        "raised": "45094771.40",
                        "usdraised": "644211.02",
                        "currencies": {
                            "INR": 45094771,
                            "USD": 644211,
                            "GBP": 501053,
                            "EUR": 536843,
                            "AED": 2254739,
                            "SGD": 867207,
                            "SAR": 2254739
                        }
                    },
                    "widget": {
                        "media_type": "image",
                        "file_name": "wid60a133a89542c.jpg",
                        "path": "/media/campaign/180000/180773/image/",
                        "cdn_path": "https://d1vdjc70h9nzd9.cloudfront.net/media/campaign/180000/180773/image/wid60a133a89542c.jpg"
                    },
                    "cause": {
                        "info_1": "Food amp; Hunger",
                        "info_3": "Orange",
                        "label": "Food amp; Hunger"
                    },
                    "campaigner": {
                        "id": 1463388,
                        "fname": "Pooja Reddy",
                        "lname": null,
                        "full_name": "Pooja Reddy ",
                        "disable_foreign_donation": 0,
                        "no_80g": false,
                        "user_details_id": 1470493,
                        "entity_type": "individual",
                        "tax_benefit": false,
                        "avtar": {
                            "entity_type_id": 1463388,
                            "entity_type": "individual",
                            "media_type": "image",
                            "file_name": "60efdb6db4758.jpg",
                            "path": "/media/individual/1463000/1463388/image/",
                            "cdn_path": "https://d1vdjc70h9nzd9.cloudfront.net/media/individual/1463000/1463388/image/60efdb6db4758.jpg"
                        }
                    },
                    "taxexempted": {
                        "id": 1505699,
                        "no_80g": true,
                        "entity_type": "ngo",
                        "tax_benefit": true
                    },

... and so on.
}
 

Комментарии:

1. Большое спасибо. Пожалуйста, объясните, где вы нашли список запросов, который находится в переменной данных?

2. @SachinGupta Я открыл https://www.ketto.org/crowdfunding/fundraisers вкладку Инструменты разработчика Firefox -> Сеть (в Chrome есть что-то похожее) и увидел там запрос (со всеми необходимыми параметрами).

3. Я видел это в firefox, но не смог отформатировать его, как вы. Как тебе это удалось?