python beautiful soup元内容标签

标签 python beautifulsoup

我正在尝试从包含以下 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)

打印(book_prices) yield :

[<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) 产生“无”。

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

最佳答案

book_price.meta 将匹配图书价格 block 内的第一个 meta 标记。第一个 meta 标记文本是“空” - 这就是为什么你会得到一个空字符串:

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

相反,获取content属性值:

book_price.meta["content"]

关于python beautiful soup元内容标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32812706/

相关文章:

python - 类型错误 : 'Styler' object is not subscriptable

python - Python 中的嵌入式 Web 服务器?

Python BeautifulSoup 提取特定的 URL

python - 如何提取标签之间的所有文本?

python - 如何仅抓取特定单词

python - beautifulsoup - 删除一行代码

Python ducktyping 感觉笨拙这是 Pythonic 吗?

python - Youtube-api::通过命令行设置元数据字段

python - 使用 pip 和 macport 安装的 virtualenv 路径错误

python - 抓取编码为 iso-8859-1 而不是 utf-8 : how do I store the correct unicode in my database? 的网站