【问题标题】:Beautiful soup meta content tag美丽的汤元内容标签
【发布时间】:2016-03-22 01:02:35
【问题描述】:
<meta itemprop="streetAddress" content="4103 Beach Bluff Rd"> 

我必须获取内容“4103 Beach Bluff Rd”。我正在尝试使用BeautifulSoup 完成此操作,所以,我正在尝试:

soup = BeautifulSoup('<meta itemprop="streetAddress" content="4103 Beach Bluff Rd"> ')

soup.find(itemprop="streetAddress").get_text()

但是我得到一个空字符串作为结果,考虑到当打印汤对象时这可能是有意义的

print soup

我明白了:

<html><head><meta content="4103 Beach Bluff Rd" itemprop="streetAddress"/> </head></html>

显然我想要的数据在'元内容'标签中,我怎样才能得到这个数据?

【问题讨论】:

    标签: python html beautifulsoup html-parsing


    【解决方案1】:

    soup.find(itemprop="streetAddress").get_text()

    您正在获取匹配元素的文本。相反,获取“内容”属性值

    soup.find(itemprop="streetAddress").get("content")
    

    这是可能的,因为BeautifulSoup 提供了dictionary-like interface to tag attributes

    您可以通过将标签视为字典来访问标签的属性。

    演示:

    >>> from bs4 import BeautifulSoup
    >>>
    >>> soup = BeautifulSoup('<meta itemprop="streetAddress" content="4103 Beach Bluff Rd"> ')
    >>> soup.find(itemprop="streetAddress").get_text()
    u''
    >>> soup.find(itemprop="streetAddress").get("content")
    '4103 Beach Bluff Rd'
    

    【讨论】:

      猜你喜欢
      • 2015-12-25
      • 2018-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-05
      • 1970-01-01
      • 1970-01-01
      • 2014-10-16
      相关资源
      最近更新 更多