2017-11-03 25 views
0

htmlを含むdescription要素内にある画像のソースを選ぶように、以下のコードをどのように変更すればよいですか?現時点では要素の中からフルテキストを取得するだけで、imgタグのソースを取得する方法を変更する方法がわかりません。要素内のimg srcのxpath

print(description.xpath("//img/@src")) 

'なし'

XML構造である私を与えない:

<guides> 
<guide> 
    <id>guide 1</id> 
    <group> 
    <id></id> 
    <type></type> 
    <name></name> 
    </group> 
    <pages> 
     <page> 
      <id>page 1</id> 
      <name></name> 
      <description>&lt;p&gt;Some text. &lt;br /&gt;&lt;img 
      width=&quot;81&quot; 
      src=&quot;http://www.example.com/img.jpg&quot; 
      alt=&quot;wave&quot; height=&quot;63&quot; style=&quot;float: 
       right;&quot; /&gt;&lt;/p&gt;</description> 
      <boxes> 
       <box> 
        <id></id> 
        <name></name> 
        <type></type> 
        <map_id></map_id> 
        <column></column> 
        <position></position> 
        <hidden></hidden> 
        <created></created> 
        <updated></updated> 
        <assets> 
         <asset> 
          <id></id> 
          <name></name> 
          <type></type> 
         <description>&lt;img src=&quot;https://www.example.com/image.jpg&quot; alt=&quot;image&quot; height=&quot;42&quot; width=&quot;42&quot;&gt;</description> 
          <url/> 
          <owner> 
           <id></id> 
           <email></email> 
           <first_name></first_name> 
           <last_name></last_name> 
          </owner> 
         </asset> 
        </assets> 
       </box> 
      </boxes> 
     </page> 
    </pages> 
</guide> 

答えて

1

description要素の内容はHTMLです。それを解析するにはさまざまな方法がありますが、そのうちの1つはhtmlからlxmlまでです。コメントに応答して

>>> description.text 
'<img src="https://www.example.com/image.jpg" alt="image" height="42" width="42">' 
>>> from lxml import html 
>>> img = html.fromstring(description.text) 
>>> img.attrib['src'] 
'https://www.example.com/image.jpg' 

編集、:

>>> from lxml import etree, html 
>>> tree = etree.parse('temp.xml') 
>>> for guide in tree.xpath('guide'): 
...  '---', guide.xpath('id')[0].text 
...  for pages in guide.xpath('.//pages'): 
...   for page in pages: 
...    '------', page.xpath('id')[0].text 
...    for description in page.xpath('.//asset/description'): 
...     '---------', html.fromstring(description.text).attrib['src'] 
... 
('---', 'guide 1') 
('------', 'page 1') 
('---------', 'https://www.example.com/image.jpg') 

編集:例外の取り扱い。

11月9日コメントへの対応、

try: 
    '---------', html.fromstring(description.text).attrib['src'] 

except KeyError: 
    '--------- No image URL present' 

編集で

'---------', html.fromstring(description.text).attrib['src'] 

を交換してください:第2ガイド要素がまったくHTMLを含まないXMLファイルの

from lxml import etree, html 
tree = etree.parse('guides.xml') 
for guide in tree.xpath('guide'): 
    print('---', guide.xpath('id')[0].text) 
    for pages in guide.xpath('.//pages'): 
     for page in pages: 
      print('------', page.xpath('id')[0].text) 
      for description in page.xpath('.//asset/description'): 
       try: 
        print('---------', html.fromstring(description.text).attrib['src']) 
       except TypeError: 
        print('--------- no src identifiable') 
       except KeyError: 
        print('--------- no src identifiable') 

出力、および3番目にsrc属性のないHTMLが含まれています。

--- guide 1 
------ page 1 
--------- https://www.example.com/image.jpg 
--- guide 2 
------ page 1 
--------- no src identifiable 
--- guide 3 
------ page 1 
--------- no src identifiable 
--- guide 4 
------ page 1 
--------- https://www.example.com/image.jpg 
+0

を返すの負荷を返します。これをforループに組み込む方法はありますか? – podusmonens

+0

編集をご覧ください。 –

+0

description要素の1つに画像URLが含まれていない場合でも解析は続行されますか、または停止しますか? XML上で実行すると、「KeyError: 'src'」が表示されます。 – podusmonens

0

あなたに

>>> from lxml import etree 
>>> tree = etree.parse('temp.xml') 
>>> for guide in tree.xpath('guide'): 
...  '---', guide.xpath('id')[0].text 
...  for pages in guide.xpath('.//pages'): 
...   for page in pages: 
...    '------', page.xpath('id')[0].text 
...    for description in page.xpath('.//asset/description'): 
...     '---------', description.text 

は、私はまた、最後にこれを試してみました試すことができるthis解:

description.xpath("//img/@src") 
+0

これは単に空の[] – podusmonens

+0

description.textは私のテキストが、description.xpath( "// IMG/@のSRC")などの画像のURLを与える理由を私は理解していない[] – podusmonens