teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

表現を修正

2020/08/07 00:15

投稿

Daregada
Daregada

スコア11990

answer CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  と書いてあるので、lxmlは使わないで行きます。
4
4
 
5
- 名前空間(今回だとns2)を含むxmlから要素を取得する場合、xml.etree.ElementTreeでは、名前空間のURIを直接`{URI}要素名`のように書くか、`名前空間:要素名`と書いて、第2引数で名前空間のURIを指定します。
5
+ 名前空間(今回だとns2)を含むxmlから要素を取得する場合、xml.etree.ElementTreeのfindでは、名前空間のURIを直接`{URI}要素名`のように書くか、`名前空間:要素名`と書いて、第2引数で名前空間のURIを指定します。
6
6
 
7
7
  ```Python
8
8
  print(product[1][0].find("{http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd}PackageQuantity").text)

1

補足を追加

2020/08/07 00:15

投稿

Daregada
Daregada

スコア11990

answer CHANGED
@@ -8,4 +8,7 @@
8
8
  print(product[1][0].find("{http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd}PackageQuantity").text)
9
9
 
10
10
  print(product[1][0].find("ns2:PackageQuantity", {'ns2': 'http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd'}).text)
11
- ```
11
+ ```
12
+
13
+ 参考:
14
+ [ElementTreeやlxmlで名前空間を含むXMLの要素を取得する - orangain flavor](https://orangain.hatenablog.com/entry/namespaces-in-xpath)