【问题标题】:python beautiful soup meta content tagpython 美丽的汤元内容标签
【发布时间】:2015-12-25 02:06:48
【问题描述】:

我正在尝试从包含以下 HTML 的网站中提取价格:

<div class="book-block-price " itemprop="offers" itemtype="http://schema.org/Offer" itemscope>
<meta itemprop="price" content="29.99"/>
<meta itemprop="price" content=""/>
    $ 29.99         </div>

我正在使用以下 Beautiful Soup 代码:

book_prices = soup_packtpage.find_all(class_="book-block-price ")
print(book_prices)
for book_price in book_prices:
    printable_version_price = book_price.meta.string
    print(printable_version_price)

print(book_prices) 产量:

[<div class="book-block-price " itemprop="offers" itemscope=""    itemtype="http://schema.org/Offer">
<meta content="29.99" itemprop="price"/>
<meta content="" itemprop="price"/>
            $ 29.99     

print(printable_version_price) 产生“无”。

如何处理元标记?还是我有其他问题?

【问题讨论】:

    标签: python beautifulsoup


    【解决方案1】:

    book_price.meta 将匹配图书价格块内的第一个 meta 标签。而这第一个meta 标记文本是“空的”——这就是为什么你会打印一个空字符串:

    <meta itemprop="price" content="29.99"/>
    

    相反,获取content 属性值:

    book_price.meta["content"]
    

    【讨论】:

      【解决方案2】:

      您可以使用lxmletree(伪代码,但应该足以让您继续):

      from lxml import etree
      doc = etree.parse(x) # where x is a file-like object, or parseString if x is a string.
      print doc.xpath('//meta[itemprop="price"]/text()')
      

      【讨论】:

        猜你喜欢
        • 2016-03-22
        • 1970-01-01
        • 2018-07-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多