【问题标题】:BeautifulSoup does not parse content of the tag like first-name that contains '-'BeautifulSoup 不会解析标签的内容,例如包含“-”的名字
【发布时间】:2013-10-06 21:59:47
【问题描述】:

您好,我有如下回复

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<person>
<first-name>hede</first-name>
<last-name>hodo</last-name>
<headline>Python Developer at hede</headline>
<site-standard-profile-request>
<url>http://www.linkedin.com/profile/view?id=hede&amp;authType=godasd*</url>
</site-standard-profile-request>
</person>

我想解析从linkedin api返回的内容。

我正在使用如下所示的美丽汤

ipdb> hede = BeautifulSoup(response.content)
ipdb> hede.person.headline
<headline>Python Developer at hede</headline>

但是当我这样做时

ipdb> hede.person.first-name
*** NameError: name 'name' is not defined

有什么想法吗?

【问题讨论】:

  • BeautifulSoup 是一个 HTML 解析器,为此任务使用一个实际的 XML 解析器。

标签: python xml beautifulsoup


【解决方案1】:

Python 属性名称不能包含连字符。 而是使用

hede.person.findChild('first-name')

另外,要使用 BeautifulSoup 解析 XML,请使用

hede = bs.BeautifulSoup(content, 'xml')

或者如果你安装了lxml,

hede = bs.BeautifulSoup(content, 'lxml')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-29
    • 2019-03-22
    • 1970-01-01
    • 2011-02-09
    • 2014-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多