2017-01-30 8 views
1

<br>を無視して、異なるタグと内容を異なる行に印刷できるように、タグを(1行で)分割しています。xmlタグをlxmlモジュールで分割する

コンテンツの数行のスニペット私は分解しようとしています。

<stats>+40 Ability Power<br>+25 Magic Resist<br>+20% Cooldown Reduction<br><mana>+75% Base Mana Regen </mana></stats><br><br><unique>UNIQUE Passive:</unique> Gain 20% of the <a href='premitigation'><font color='#6666FF'><u>premitigation</u></font></a> damage dealt to champions as Blood Charges, up to <levelScale>100 - 250</levelScale> max. Healing or shielding another ally consumes charges to heal them, up to the original effect amount.<br><unique>UNIQUE Passive - Harmony:</unique> Grants bonus % Base Health Regen equal to your bonus % Base Mana Regen.<br><br><rules>(Maximum amount of Blood Charges stored is based on level. Healing amplification is applied to the total heal value.)</rules> 
<stats>+10% Critical Strike Chance</stats> 
<stats>+45 Attack Damage<br>+10% Life Steal</stats><br><br><unique>UNIQUE Passive:</unique> Basic attacks grant +6 Attack Damage and +1% Life Steal for 8 seconds on hit (effect stacks up to 5 times). 
<stats>+300 Health<br>+50 Attack Damage<br>+20% Cooldown Reduction</stats><br><br><unique>UNIQUE Passive:</unique> Dealing physical damage to an enemy champion Cleaves them, reducing their Armor by 5% for 6 seconds (stacks up to 6 times, up to 30%).<br><unique>UNIQUE Passive - Rage:</unique> Dealing physical damage grants 20 movement speed for 2 seconds. Assists on Cleaved enemy champions or kills on any unit grant 60 movement speed for 2 seconds instead. This Movement Speed is halved for ranged champions. 

しかし、私はlxmlモジュールからXMLのような文字列を解析してみてください。あなたが表示さ

ValueError: Invalid tag name "<stats>+40 Attack Damage<br>+80 Ability Power</stats><br><br><unique>UNIQUE Passive:</unique> Heal for 15% of damage dealt. This is 33% as effective for Area of Effect damage.<br><active>UNIQUE Active - Lightning Bolt:</active> Deals 250 (+30% of Ability Power) magic damage and slows the target champion's Movement Speed by 40% for 2 seconds (40 second cooldown, shared with other <font color='#9999FF'><a href='itembolt'>Hextech</a></font> items)." 

答えて

0

入力がXMLではありません。

root = etree.Element(string) 

は、それは私にエラーを与えます。 XMLパーサーはこれを読み取ることができません。

HTMLパーサーを使用すると、より広範囲の入力を受け入れることができます。 http://lxml.de/parsing.html#parsing-htmlを参照してください。

from lxml import etree 
from io import StringIO 

test_string = "<stats>+40 Ability Power<br>+25 Magic Resist<br>+20% Cooldown Reduction<br><mana>+75% Base Mana Regen </mana></stats><br><br><unique>UNIQUE Passive:</unique> Gain 20% of the <a href='premitigation'><font color='#6666FF'><u>premitigation</u></font></a> damage dealt to champions as Blood Charges, up to <levelScale>100 - 250</levelScale> max. Healing or shielding another ally consumes charges to heal them, up to the original effect amount.<br><unique>UNIQUE Passive - Harmony:</unique> Grants bonus % Base Health Regen equal to your bonus % Base Mana Regen.<br><br><rules>(Maximum amount of Blood Charges stored is based on level. Healing amplification is applied to the total heal value.)</rules>" 
html_file = StringIO(test_string) 

parser = etree.HTMLParser() 
doc = etree.parse(html_file, parser) 

print(doc) 
# prints: <lxml.etree._ElementTree object at 0x03036A58> 

その後、lxmlのすべてのメソッドを使用してドキュメントツリーを検索および変更できます。

注:StringIOは、文字列をファイルのように使用できるようにするために、上記の例のためにのみ必要です。既にファイルがある場合は、StringIOは必要ありません。ただファイルを直接使用してください。

ウェブスクレイプソリューションを実際に探している場合は、それを行うライブラリを見てください。 ScrapypyQueryが気になると、彼らはあなたのためにすべての解析を行い、ページ上のデータにアクセスするためのより良いインターフェースを提供します。

+0

本当に良い答えです、ありがとうございます。 – SkinnyTok

関連する問題