2017-01-30 51 views
1

我試圖分開標籤(在一行上),以便我可以在不同的行上打印不同的標籤和內容,同時忽略<br>用lxml模塊打破xml標籤

幾行內容的片段我試圖分開。

<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類字符串。

root = etree.Element(string) 

它給我的錯誤:

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。 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