0

尝试以下代码对我来说似乎并没有按计划完成: from beautifulsoup import BeautifulSoup

definition = """From encyclopedia:\n<i></i><p>Infobox Country<br>fullcountryname=Thailand  &#x0E23;&#x0E32;&#x0E0A;&#x0E2D;&#x0E32;&#x0E13;&#x0E32;&#x0E08;&#x0E31;&#x0E01;&#x0E23;&#x0E44;&#x0E17;&#x0E22;Raja-anachakra Thai <br>image_flag= Flag of Thailand.svg <br>image_coa= Coat of arms of Thailand.png <br>image_location= LocationThailand.png <br>nationalmotto= none <br>nationalsong= Phleng Chat <br>nationalflower= n/a <br>nationalanimal= n/a <br>officiallanguages= Thai (<r><i>Thai language</i></r>) <br>populationtotal= 65,444,371 <br>populationrank= 19 <br>populationdensity= 127 <br>countrycapital= <r>Bangkok</r> <br>countrylargestcity= <r>Bangkok</r> <br>areatotal= 514,000 <br>arearank= 49 <br>areawater= n/a <br>areawaterpercent= 0.4 <br>establishedin= <r>April 7</r>, <r>1782</r> <br>leadertitlename=    <br>currency= <r>Baht</r> <br>utcoffset= +7 <br>dialingcode= 66 <br>internettld= .th<p><b>Thailand</b> is a <r>country</r> in Southeast <r>Asia</r>.  Its edges touch <r>Laos</r>, <r>Cambodia</r>, <r>Malaysia</r>, and <r>Myanmar</r> (which is also called Burma.) Thailand was called Siam until 1949."""

print BeautifulSoup(definition).find('p[1]').text

这不会返回任何东西。我确定这是我使用 BeautifulSoup 的语法错误,有没有人知道我怎么能简单地得到:

Infobox Country
fullcountryname=Thailand Raja-anachakra Thai 
image_flag= Flag of Thailand. svg 
image_coa= Coat of arms of Thailand. png 
image_location= LocationThailand. png 
nationalmotto= none 
nationalsong= Phleng Chat 
nationalflower= n/a 
nationalanimal= n/a 
officiallanguages= Thai (Thai language) 
populationtotal= 65,444,371 
populationrank= 19 
populationdensity= 127 
countrycapital= Bangkok 
countrylargestcity= Bangkok 
areatotal= 514,000 
arearank= 49 
areawater= n/a 
areawaterpercent= 0. 4 
establishedin= April 7, 1782 
leadertitlename=  
currency= Baht 
utcoffset= +7 
dialingcode= 66 
internettld= . th

谢谢 :)

编辑:如果我能得到单词“Infobox”和最后一个之间的文本,我实际上更愿意

标签,以便我可以使用脚本来解析实时维基百科页面。

4

3 回答 3

4

find()只匹配第一个元素,所以只需使用find('p').

>>>print BeautifulSoup(definition).find('p').text
Infobox Countryfullcountryname=Thailand  &#x0E23;&#x0E32;&#x0E0A;&#x0E2D;&#x0E32;&#x0E13;&#x0E32;&#x0E08;&#x0E31;&#x0E01;&#x0E23;&#x0E44;&#x0E17;&#x0E22;Raja-anachakra Thaiimage_flag= Flag of Thailand.svgimage_coa= Coat of arms of Thailand.pngimage_location= LocationThailand.pngnationalmotto= nonenationalsong= Phleng Chatnationalflower= n/anationalanimal= n/aofficiallanguages= Thai (Thai language)populationtotal= 65,444,371populationrank= 19populationdensity= 127countrycapital=Bangkokcountrylargestcity=Bangkokareatotal= 514,000arearank= 49areawater= n/aareawaterpercent= 0.4establishedin=April 7,1782leadertitlename=currency=Bahtutcoffset= +7dialingcode= 66internettld= .th
于 2012-04-28T11:55:42.110 回答
0

您正在使用 Beautiful Soup 不支持的 XPath 语法。Lattyware 的回答是正确的。至于您编辑中的问题,您可以使用 Beautiful Soup 4 的.stripped_strings 生成器来获得您想要的大致内容。一些示例代码:

from bs4 import BeautifulSoup
soup = BeautifulSoup(definition)

import re
infobox_start = re.compile("^Infobox") 

start_at = soup.find(text=infobox_start)
for string in start_at.parent.stripped_strings:
    print string
于 2012-04-28T11:59:03.657 回答
0

如果它是您正在寻找的信息框,您可能会发现DBpedia为您提供了强大的程序化和更稳定的数据访问。当然,Wikipedia API 和python wikitools也提供了这个。

这两种解决方案都有一个学习曲线,但可能比抓取更稳定和尊重网站。

于 2012-05-02T10:48:56.013 回答