2014-01-27 33 views
1

林试图从网页上的文字与Python 3.3,然后通过文字某些字符串搜索。当我找到匹配的字符串时,我需要保存以下文本。例如,我拿这个页面:http://gatherer.wizards.com/Pages/Card/Details.aspx?name=Dark%20Prophecy 我需要保存卡片信息中每个类别(卡片文本,稀有等)后的文本。 目前我使用美丽的汤,但get_text导致UnicodeEncodeError,并没有返回一个可迭代的对象。下面是相关代码:HTML解析文本在Python 3

   urlStr = urllib.request.urlopen('http://gatherer.wizards.com/Pages/Card/Details.aspx?name=' + cardName).read() 

       htmlRaw = BeautifulSoup(urlStr) 

       htmlText = htmlRaw.get_text 

       for line in htmlText: 
        line = line.strip() 
        if "Converted Mana Cost:" in line: 
         cmc = line.next() 
         message += "*Converted Mana Cost: " + cmc +"* \n\n" 
        elif "Types:" in line: 
         type = line.next() 
         message += "*Type: " + type +"* \n\n" 
        elif "Card Text:" in line: 
         rulesText = line.next() 
         message += "*Rules Text: " + rulesText +"* \n\n" 
        elif "Flavor Text:" in line: 
         flavor = line.next() 
         message += "*Flavor Text: " + flavor +"* \n\n" 
        elif "Rarity:" in line: 
         rarity == line.next() 
         message += "*Rarity: " + rarity +"* \n\n" 

回答

1

考虑使用lxml and xpath,而不是,您将能够做这样的事情:

>>> from lxml import html 
>>> root = html.parse("http://gatherer.wizards.com/Pages/Card/Details.aspx?name=Dark%20Prophecy") 
>>> root.xpath('//div[contains(text(), "Flavor Text")]/following-sibling::div/div/i/text()') 
['When the bog ran short on small animals, Ekri turned to the surrounding farmlands.'] 
+0

如何在Windows上安装呢?网站上的说明似乎只是说下载它,但这并不奏效 – CrazyBurrito