XML とは何ですか? XML は Extensible Markup Language を意味し、構造化データを保存し、項目をグループ化する必要があります。 XML マークアップ言語では、任意の名前のタグを作成できます。 XML の最も一般的な例 - サイトマップと RSS フィード。
XML ファイルの例:
<breakfast_menu> <food> <name>Belgian Waffles</name> <price>.95</price> <description>Two of our famous Belgian Waffles with plenty of real maple syrup</description> <calories>650</calories> </food> <food> <name>Strawberry Belgian Waffles</name> <price>.95</price> <description>Light Belgian waffles covered with strawberries and whipped cream</description> <calories>900</calories> </food> <food> <name>Berry-Berry Belgian Waffles</name> <price>.95</price> <description>Light Belgian waffles covered with an assortment of fresh berries and whipped cream</description> <calories>900</calories> </food> <food> <name>French Toast</name> <price>.50</price> <description>Thick slices made from our homemade sourdough bread</description> <calories>600</calories> </food> <food> <name>Homestyle Breakfast</name> <price>.95</price> <description>Two eggs, bacon or sausage, toast, and our ever-popular hash browns</description> <calories>950</calories> </food> </breakfast_menu>
この例では、ファイルには、食品カテゴリを含む breakfast_menu グローバル タグが含まれており、すべての食品カテゴリには名前、価格、説明、カロリー タグが含まれています。
ここで、XML と Python リクエスト ライブラリの操作方法を学び始めます。まず、作業環境を準備する必要があります。
新しいプロジェクトと仮想環境を作成するには、python3-virtualenv パッケージをインストールします。プロジェクトごとの分離要件が必要です。 Debian/Ubuntu でのインストール:
sudo apt install python3 python3-virtualenv -y
プロジェクトフォルダーの作成:
mkdir my_project cd my_project
env という名前のフォルダーを使用して Python 仮想環境を作成します:
python3 -m venv env
仮想環境をアクティブ化します:
source env/bin/activate
PIP の依存関係をインストールします:
pip3 install requests
コードの作成を始めましょう。
main.py ファイルを作成し、以下のコードを挿入します:
import requests import xml.etree.ElementTree as ET request = requests.get('https://www.w3schools.com/xml/simple.xml') root = ET.fromstring(request.content) for item in root.iter('*'): print(item.tag)
このコード スニペットは、すべての内部タグを見つけるのに役立ちます。
このコードの出力:
(env) user@localhost:~/my_project$ python3 main.py breakfast_menu food name price description calories food name price description calories food name price description calories food name price description calories food name price description calories
ここで、内部要素から値を取得するコードを作成します。 main.py ファイルを開き、以前のコードを次のコードに置き換えます:
import requests import xml.etree.ElementTree as ET request = requests.get('https://www.w3schools.com/xml/simple.xml') root = ET.fromstring(request.content) for item in root.iterfind('food'): print(item.findtext('name')) print(item.findtext('price')) print(item.findtext('description')) print(item.findtext('calories'))
次の結果を受け取りました:
(env) user@localhost:~/my_project$ python3 main.py Belgian Waffles .95 Two of our famous Belgian Waffles with plenty of real maple syrup 650 Strawberry Belgian Waffles .95 Light Belgian waffles covered with strawberries and whipped cream 900 Berry-Berry Belgian Waffles .95 Light Belgian waffles covered with an assortment of fresh berries and whipped cream 900 French Toast .50 Thick slices made from our homemade sourdough bread 600 Homestyle Breakfast .95 Two eggs, bacon or sausage, toast, and our ever-popular hash browns 950
最後のステップでは、読みやすくするために出力データを整形します。
import requests import xml.etree.ElementTree as ET request = requests.get('https://www.w3schools.com/xml/simple.xml') root = ET.fromstring(request.content) for item in root.iterfind('food'): print('Name: {}. Price: {}. Description: {}. Calories: {}'.format(item.findtext('name'), item.findtext('price'), item.findtext('description'), item.findtext('calories')))
ここでの出力:
(env) user@localhost:~/my_project$ python3 main.py Name: Belgian Waffles. Price: .95. Description: Two of our famous Belgian Waffles with plenty of real maple syrup. Calories: 650 Name: Strawberry Belgian Waffles. Price: .95. Description: Light Belgian waffles covered with strawberries and whipped cream. Calories: 900 Name: Berry-Berry Belgian Waffles. Price: .95. Description: Light Belgian waffles covered with an assortment of fresh berries and whipped cream. Calories: 900 Name: French Toast. Price: .50. Description: Thick slices made from our homemade sourdough bread. Calories: 600 Name: Homestyle Breakfast. Price: .95. Description: Two eggs, bacon or sausage, toast, and our ever-popular hash browns. Calories: 950
出典資料:
W3Schools から取得した XML ファイルの例
私の投稿は役に立ちましたか? Patreon で私をサポートしていただけます。
以上がPython リクエスト ライブラリでの XML の操作の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

Pythonlistscanstoreanydatatype,arraymodulearraysstoreonetype,andNumPyarraysarefornumericalcomputations.1)Listsareversatilebutlessmemory-efficient.2)Arraymodulearraysarememory-efficientforhomogeneousdata.3)NumPyarraysareoptimizedforperformanceinscient

heouttemptemptostoreavure ofthewrongdatatypeinapythonarray、yure counteractypeerror.thisduetothearraymodule'sstricttypeeencultionyを使用します

PythonListSarePartOfThestAndardarenot.liestareBuilting-in、versatile、forStoringCollectionsのpythonlistarepart。

theScriptisrunningwithwrongthonversionduetorectRectDefaultEntertersettings.tofixthis:1)CheckthedededefaultHaulthonsionsingpython - versionorpython3-- version.2)usevirtualenvironmentsbycreatingonewiththon3.9-mvenvmyenv、andverixe

PythonArraysSupportVariousoperations:1)SlicingExtractsSubsets、2)Appending/ExtendingAdddesements、3)inSertingSelementSatspecificpositions、4)remvingingDeletesements、5)sorting/verversingsorder、and6)listenionsionsionsionsionscreatenewlistsebasedexistin

numpyarraysAressertialentionsionceivationsefirication-efficientnumericalcomputations andDatamanipulation.theyarecrucialindatascience、mashineelearning、物理学、エンジニアリング、および促進可能性への適用性、scaledatiencyを効率的に、forexample、infinancialanalyyy

UseanArray.ArrayOverAlistinPythonは、Performance-criticalCode.1)homogeneousdata:araysavememorywithpedelements.2)Performance-criticalcode:Araysofterbetterbetterfornumerumerumericaleperations.3)interf

いいえ、notallistoperationSaresuptedbyarrays、andviceversa.1)arraysdonotsupportdynamicoperationslikeappendorintorintorinsertizizing、whosimpactsporformance.2)リスト


ホットAIツール

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

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

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

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

人気の記事

ホットツール

AtomエディタMac版ダウンロード
最も人気のあるオープンソースエディター

SecLists
SecLists は、セキュリティ テスターの究極の相棒です。これは、セキュリティ評価中に頻繁に使用されるさまざまな種類のリストを 1 か所にまとめたものです。 SecLists は、セキュリティ テスターが必要とする可能性のあるすべてのリストを便利に提供することで、セキュリティ テストをより効率的かつ生産的にするのに役立ちます。リストの種類には、ユーザー名、パスワード、URL、ファジング ペイロード、機密データ パターン、Web シェルなどが含まれます。テスターはこのリポジトリを新しいテスト マシンにプルするだけで、必要なあらゆる種類のリストにアクセスできるようになります。

ドリームウィーバー CS6
ビジュアル Web 開発ツール

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

DVWA
Damn Vulnerable Web App (DVWA) は、非常に脆弱な PHP/MySQL Web アプリケーションです。その主な目的は、セキュリティ専門家が法的環境でスキルとツールをテストするのに役立ち、Web 開発者が Web アプリケーションを保護するプロセスをより深く理解できるようにし、教師/生徒が教室環境で Web アプリケーションを教え/学習できるようにすることです。安全。 DVWA の目標は、シンプルでわかりやすいインターフェイスを通じて、さまざまな難易度で最も一般的な Web 脆弱性のいくつかを実践することです。このソフトウェアは、

ホットトピック









