简体   繁体   中英

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM