Python 美化组:空条目?不再工作了?
我想使用BeautifulSoup将xml google联系人转换为csv。我有一个脚本,工作得很好,但它现在停止工作,没有对源代码做任何更改。也许美丽的乌苏改变了 因此,xml文件包含如下条目:Python 美化组:空条目?不再工作了?,python,beautifulsoup,Python,Beautifulsoup,我想使用BeautifulSoup将xml google联系人转换为csv。我有一个脚本,工作得很好,但它现在停止工作,没有对源代码做任何更改。也许美丽的乌苏改变了 因此,xml文件包含如下条目: <entry ns1:etag=""RcDVSLt7I2AQEQAM.""> <category scheme="http://schemas.google.com/g/..." /> <title>Pepe Estropa
<entry ns1:etag=""RcDVSLt7I2AQEQAM."">
<category scheme="http://schemas.google.com/g/..." />
<title>Pepe Estropajo</title>
</edited>
<name>
<fullName>Pepe</fullName>
<givenName>...</givenName>
<familyName>Estropajo</familyName>
</name>
</entry>
问题是这个名字没有。为什么找不到标题?
我要求脚本打印姓名,它打印了:
<entry ns1:etag='"Rng-cDVSLyt7I2A9Wh5QEEUNQAM."'></entry>
里面没有孩子
我做错了什么?这件作品怎么样
>>> entries = """<entry ns1:etag=""RcDVSLt7I2AQEQAM."">
... <category scheme="http://schemas.google.com/g/..." />
... <title>Pepe Estropajo</title>
... </edited>
... <name>
... <fullName>Pepe</fullName>
... <givenName>...</givenName>
... <familyName>Estropajo</familyName>
... </name>
... </entry>"""
>>>
>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup(entries)
>>>
>>> for entry in soup.findAll('entry'):
... print entry.find('title').getText()
...
Pepe Estropajo
如何解析XML?BeautifulSoupsource,'xml'或其他?@MartijnPieters:像这样:agxml=/home/gato/varios/google-contacts.xml soup=BeautifulSoupopenagxml,'r'。读取,fromEncoding=Latin1,然后以HTML而不是xml的形式打开文件。在这种情况下,将应用不同的解析规则。您是否安装了lxml?这是第三组还是第四组?你的代码对我来说很好。我用的是beautifulsoup4。我使用bs4导入Beautifulsoup@LuisA.Florit:那么您正在使用BeautifulSoup 3。从bs4导入BeautifulSoup.com使用。我从文件中读取内容。上面Martin和salman已经回答了这个问题:BS3确实存在问题,BS4工作正常。无论如何谢谢你!!
>>> entries = """<entry ns1:etag=""RcDVSLt7I2AQEQAM."">
... <category scheme="http://schemas.google.com/g/..." />
... <title>Pepe Estropajo</title>
... </edited>
... <name>
... <fullName>Pepe</fullName>
... <givenName>...</givenName>
... <familyName>Estropajo</familyName>
... </name>
... </entry>"""
>>>
>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup(entries)
>>>
>>> for entry in soup.findAll('entry'):
... print entry.find('title').getText()
...
Pepe Estropajo