Python リクエスト ライブラリを使用したリダイレクトされた URL の取得
Web スクレイピングと自動化の領域では、発生するリダイレクトを追跡することが必要になることがよくあります。 Webページにアクセスするとき。 Python リクエスト ライブラリは、リダイレクトをナビゲートする機能など、HTTP リクエストを管理するための包括的な機能を提供します。この記事は、Requests ライブラリを使用してリダイレクトされた URL を取得する方法を明確にし、コミュニティで提起されたクエリに対処することを目的としています。
問題:
スクリプト内では、allow_redirects =True フラグは、リダイレクトを自動的に追跡するように設定されます。ただし、ユーザーはリクエストのリダイレクト先となる最終 URL を決定したいと考えています。疑問が生じます: この情報をプログラムでどのように取得できるでしょうか?
解決策:
解決策は、リクエストによって提供される response.history 属性を利用することにあります。この属性には、最終宛先に至るリダイレクトの過程全体を含む応答のリストが保存されます。最終 URL にアクセスするには、response.url プロパティを使用できます。
プロセスを示すコード スニペットは次のとおりです:
response = requests.get(someurl) if response.history: print("Request was redirected") for resp in response.history: print(resp.status_code, resp.url) print("Final destination:") print(response.status_code, response.url) else: print("Request was not redirected")
デモ:
複数の処理を実行するように構成された Web サイトに対してリクエストが行われる次の例を考えてみましょう。 redirects:
import requests response = requests.get('http://httpbin.org/redirect/3') for resp in response.history: print(resp.status_code, resp.url) print(response.status_code, response.url)
出力:
302 http://httpbin.org/redirect/3 302 http://httpbin.org/redirect/2 302 http://httpbin.org/redirect/1 200 http://httpbin.org/get
出力から明らかなように、コードは各リダイレクトのステータス コードと URL、および最終的な宛先を正常に記録します。
以上がPython リクエスト ライブラリを使用してリダイレクトされた URL を取得するにはどうすればよいですか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

forhandlinglaredataSetsinpython、usenumpyArrays forbetterperformance.1)numpyarraysarememory-effictientandfasterfornumericaloperations.2)nusinnnnedarytypeconversions.3)レバレッジベクトル化は、測定済みのマネージメーシェイメージーウェイズデイタイです

inpython、listsusedynamicmemoryallocation with allocation、whilenumpyArraysalocatefixedmemory.1)listsallocatemorememorythanneededededinitivative.2)numpyArrayasallocateexactmemoryforements、rededicablebutlessflexibilityを提供します。

inpython、youcanspecthedatatypeyfelemeremodelernspant.1)usenpynernrump.1)usenpynerp.dloatp.ploatm64、フォーマーpreciscontrolatatypes。

numpyisessentialfornumericalcomputinginpythonduetoitsspeed、memory efficiency、andcomprehensivematicalfunctions.1)それは、performsoperations.2)numpyArraysaremoremory-efficientthanpythonlists.3)Itofderangeofmathematicaloperty

contiguousMemoryAllocationisucial forArraysは、ForeffienceAndfastelementAccess.1)iteenablesConstantTimeAccess、O(1)、DuetodirectAddresscalculation.2)itemprovesefficiencyByAllowingMultiblementFechesperCacheLine.3)itimplifieMememm

slicingapythonlistisdoneusingtheyntaxlist [start:stop:step] .hore'showitworks:1)startisthe indexofthefirstelementtoinclude.2)spotisthe indexofthefirmenttoeexclude.3)staptistheincrementbetbetinelements

numpyallows forvariousoperationsonarrays:1)basicarithmeticlikeaddition、減算、乗算、および分割; 2)AdvancedperationssuchasmatrixMultiplication;

Arraysinpython、特にnumpyandpandas、aresentialfordataanalysis、offeringspeedandeficiency.1)numpyarraysenable numpyarraysenable handling forlaredatasents andcomplexoperationslikemoverages.2)Pandasextendsnumpy'scapabivitieswithdataframesfortruc


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

SAP NetWeaver Server Adapter for Eclipse
Eclipse を SAP NetWeaver アプリケーション サーバーと統合します。

MinGW - Minimalist GNU for Windows
このプロジェクトは osdn.net/projects/mingw に移行中です。引き続きそこでフォローしていただけます。 MinGW: GNU Compiler Collection (GCC) のネイティブ Windows ポートであり、ネイティブ Windows アプリケーションを構築するための自由に配布可能なインポート ライブラリとヘッダー ファイルであり、C99 機能をサポートする MSVC ランタイムの拡張機能が含まれています。すべての MinGW ソフトウェアは 64 ビット Windows プラットフォームで実行できます。

SublimeText3 中国語版
中国語版、とても使いやすい

メモ帳++7.3.1
使いやすく無料のコードエディター

Dreamweaver Mac版
ビジュアル Web 開発ツール
