PatrickP76
PatrickP76

Reputation: 135

python beautifulsoup findall within find

I am trying to get the text in td class 'column-1' and I am having some trouble because it has no attribute text - but it clearly does so I must be doing something wrong. Here is the code:

import urllib
import urllib.request
from bs4 import BeautifulSoup

theurl="http://vermontamerican.com/products/standard-drill-bit-extensions/"
thepage = urllib.request.urlopen(theurl)
soup = BeautifulSoup(thepage,"html.parser")

for part in soup.find_all('td'),{"class":"column-1"}:
    part1 = part.text
    print(part1)

If I take line 2 out and just print "part" above I get a result but it is giving all td not just column-1. I have also tried this but I am new so I am sure this is wrong in more ways than one.

import urllib
import urllib.request
from bs4 import BeautifulSoup

theurl="http://vermontamerican.com/products/standard-drill-bit-extensions/"
thepage = urllib.request.urlopen(theurl)
soup = BeautifulSoup(thepage,"html.parser")


for part in soup.find('tbody'),{"class":"row-hover"}:
    for part1 in part.find_all('a'):
        print(part1)

Upvotes: 2

Views: 1152

Answers (1)

alecxe
alecxe

Reputation: 473873

You are not passing the attribute selection dictionary into the find_all() function. Replace:

for part in soup.find_all('td'),{"class":"column-1"}:

with:

for part in soup.find_all('td', {"class":"column-1"}):

Now your code would produce:

17103
17104

Upvotes: 2

Related Questions