返回顶部
首页 > 资讯 > 后端开发 > Python >如何解决Python爬取网页时请求中收到响应400的问题
  • 364
分享到

如何解决Python爬取网页时请求中收到响应400的问题

2023-06-20 21:06:27 364人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

这篇文章将为大家详细讲解有关如何解决python爬取网页时请求中收到响应400的问题,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。一般情况下抓取网页时找到url中可以从 post 请求中获取数据,但得到的

这篇文章将为大家详细讲解有关如何解决python爬取网页时请求中收到响应400的问题,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

一般情况下抓取网页时找到url中可以从 post 请求中获取数据,但得到的响应是400而不是 200时,可以尝试从服务器获得正确的响应,使用JSON=参数requests进行尝试可以解决。

问题案例:

 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/indexesqueries"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&maxValuesPerFacet=10&page=0&highlightPreTag=__ais-highlight__&highlightPostTag=__%2Fais-highlight__&facets=%5B%22cause.label%22%2C%22tags%22%2C%22address%22%5D&tagFilters=&facetFilters=%5B%5B%22cause.label%3A%22%5D%2C%5B%22tags%3A%22%5D%2C%5B%22address%3A%22%5D%5D",        },        {            "indexName": "fundraiser_prod",            "params": "query=&hitsPerPage=1&maxValuesPerFacet=10&page=0&highlightPreTag=__ais-highlight__&highlightPostTag=__%2Fais-highlight__&attributesToRetrieve=%5B%5D&attributesToHighlight=%5B%5D&attributesToSnippet=%5B%5D&tagFilters=&analytics=false&clickAnalytics=false&facets=%5B%22cause.label%22%5D&facetFilters=%5B%5B%22tags%3A%22%5D%2C%5B%22address%3A%22%5D%5D",        },        {            "indexName": "fundraiser_prod",            "params": "query=&hitsPerPage=1&maxValuesPerFacet=10&page=0&highlightPreTag=__ais-highlight__&highlightPostTag=__%2Fais-highlight__&attributesToRetrieve=%5B%5D&attributesToHighlight=%5B%5D&attributesToSnippet=%5B%5D&tagFilters=&analytics=false&clickAnalytics=false&facets=%5B%22tags%22%5D&facetFilters=%5B%5B%22cause.label%3A%22%5D%2C%5B%22address%3A%22%5D%5D",        },        {            "indexName": "fundraiser_prod",            "params": "query=&hitsPerPage=1&maxValuesPerFacet=10&page=0&highlightPreTag=__ais-highlight__&highlightPostTag=__%2Fais-highlight__&attributesToRetrieve=%5B%5D&attributesToHighlight=%5B%5D&attributesToSnippet=%5B%5D&tagFilters=&analytics=false&clickAnalytics=false&facets=%5B%22address%22%5D&facetFilters=%5B%5B%22cause.label%3A%22%5D%2C%5B%22tags%3A%22%5D%5D",        },    ]}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 & Hunger",                        "info_3": "Orange",                        "label": "Food & 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.}

关于“如何解决Python爬取网页时请求中收到响应400的问题”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

--结束END--

本文标题: 如何解决Python爬取网页时请求中收到响应400的问题

本文链接: https://lsjlt.com/news/300041.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作