bLAZ bLAZ - 2 months ago 17
Python Question

BeautifulSoup: How to extract content?

on the website that I'm trying to parse are tags like:

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


I would like to get a list of their content (here it is 2526286 value). How can I do that? I tried with

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


but it returns 'None' for each tag found.

Jan Jan
Answer

You can use:

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

As whole code:

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)