【问题标题】:Issue in extracting value from xml(apple rating xml) using BEAUTIFULSOUP python使用 BEAUTIFULSOUP python 从 xml(苹果评级 xml)中提取值的问题
【发布时间】:2016-05-30 18:12:05
【问题描述】:

我正在尝试为

提取文本
<im:rating>5</im:rating>
<im:version>1.14</im:version> 

来自apple xml的xml,用于使用BeautifulSoup进行应用商店审查。

我的代码是

def getReview():
    url = "https://itunes.apple.com/rss/customerreviews/page=1/id=511376996/sortby=mostrecent/xml?l=en&cc=us" 
        source = requests.get(url)
        text = source.text
        soup = BeautifulSoup(text, 'xml')
        for l in soup.findAll('entry'):
            rate=l.find('rating')
            author=(l.find('name')).text
            appver=l.find('version')

            print(rate)
            print(author)
            print(appver)

当我使用上面的代码时,我得到了作者的文本 &

<im:rating>5</im:rating>
<im:version>1.14</im:version>

对于评级和版本,如果我使用appver=l.find('version').text,那么它会给出错误

  appver=l.find('version').text
AttributeError: 'NoneType' object has no attribute 'text'

我只想获取这些评级和版本文本的值。即评级为“5”和版本“1.14”。

需要帮助并提前致谢

【问题讨论】:

  • 你确定你真的得到了这个版本吗?我假设基于您当前代码中 appver 的错误将是 None 以获取您遇到的错误。
  • @TadhgMcDonald-Jensen 页面中存在应用程序版本,当我打印 {appver=l.find('version')} 时,它会打印 {1.14version>} 但我只想用 1.14 代替 {1.14}
  • 只是关于格式化的注释,您可以使用反引号 (`) 来表示代码块,在编辑问题时也有一个 ?为格式化提供进一步帮助的按钮。
  • 你是说发布你的代码错误吗?
  • @GauravKumar,简单的回答是第一个条目没有评级或版本

标签: python xml parsing beautifulsoup


【解决方案1】:

如果你只是想获取这些标签,一个简单的 pyparsing 解析器将在没有 BeautifulSoup 箍跳过的情况下获取它们。通过只解析给定的标签(pyparsing 的标签匹配非常全面),您可以跳过解析整个 HTML 的开销,只获取您想要的部分,并以您自己设计的简化结构将它们取回。见下文,使用 cmets 和带有 3 个条目的模拟 HTML:

from pyparsing import makeHTMLTags, SkipTo, ungroup

def get_tag_body(start_tag, end_tag):
    return ungroup(start_tag.suppress() + SkipTo(end_tag) + end_tag.suppress())

# makeHTMLTags returns a 2-tuple containing expressions for the
# corresponding start tag and end tag

rating_expr = get_tag_body(*makeHTMLTags("im:rating"))("rating")
version_expr = get_tag_body(*makeHTMLTags("im:version"))("version")

# the desired pattern is the rating_expr followed by the version_expr
search_parser = rating_expr + version_expr

# parse the posted sample
sample = """
<im:rating>5</im:rating>
<im:version>1.14</im:version> 
"""

# access the named fields using dot notation or dict key notation
results = search_parser.searchString(sample)
if results:
    for res in results:
        print("rating = {rating}, version = {version}".format_map(res))

打印:

rating = 5, version = 1.14

【讨论】:

  • 这看起来并不容易,它引发了 OP 中使用的数据的错误,它实际上并没有回答有关错误的问题,我根本不理解输出.
  • 我去掉了分散注意力的部分,只是放入了发布的样本,这对我来说不会引发错误,如发布的输出所示。
  • 我的意思是链接:itunes.apple.com/rss/customerreviews/page=1/id=511376996/… 是 OP 试图解析的确切数据,我仍然不知道这比 BeautifulSoup 更好,它肯定需要更长的时间执行。
  • 我仍然没有收到错误,即使使用从 OP 中的 URL 获取的 XML,我 确实 找到了 50 个评级-版本对。你得到了什么错误?至于这是否比 BeautifulSoup 更好,OP 可以选择 - SO 有许多建设性的答案,可以提出替代方法或库来解决已发布的问题。 (只需查看有关如何使用正则表达式解析 HTML 的所有问题,并提供 Beautiful Soup 作为替代方法。)
猜你喜欢
  • 2019-10-15
  • 1970-01-01
  • 1970-01-01
  • 2021-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-29
  • 1970-01-01
相关资源
最近更新 更多