【问题标题】:BeautifulSoup: How to extract content?BeautifulSoup:如何提取内容?
【发布时间】:2017-01-29 17:10:48
【问题描述】:

在我试图解析的网站上有如下标签:

<a class="sku" href="http://pl.farnell.com/tdk/c3225x6s0j107m250ac/capacitor-mlcc-x6s-100uf-6-3v/dp/2526286" title="2526286">2526286</a>

我想获取他们的内容列表(这里是 2526286 值)。我怎样才能做到这一点?我试过了

for node in soup.find_all('a', {'class': 'sku'}):
print(node.content)

但它为找到的每个标签返回“无”。

【问题讨论】:

  • Use node.textnode["title"]

标签: python css-selectors beautifulsoup html-parsing


【解决方案1】:

你可以使用:

for node in soup.find_all('a', {'class': 'sku'}):
    print(node.string)

作为整个代码:

from bs4 import BeautifulSoup

string = """
<div>
    <a class="sku" href="http://pl.farnell.com/tdk/c3225x6s0j107m250ac/capacitor-mlcc-x6s-100uf-6-3v/dp/2526286" title="2526286">2526286</a>
</div>
"""
soup = BeautifulSoup(string, "lxml")
for node in soup.find_all('a', {'class': 'sku'}):
    print(node.string)

【讨论】:

  • 完美运行!谢谢!
猜你喜欢
  • 2014-11-29
  • 1970-01-01
  • 1970-01-01
  • 2020-09-11
  • 1970-01-01
  • 1970-01-01
  • 2012-02-13
  • 2020-07-22
  • 1970-01-01
相关资源
最近更新 更多