找到python beautifulsoup findall

时间:2016-03-25 01:37:30

标签: python html python-3.x web-scraping beautifulsoup

我正在尝试在td class' column-1'中获取文本。我遇到了一些麻烦,因为它没有属性文本 - 但显然这样做我必须做错事。这是代码:

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)

如果我将第2行排除在外,只需打印" part"上面我得到一个结果,但它给的所有td不仅仅是第1列。 我也尝试了这个,但我是新手,所以我确信这在很多方面都是错误的。

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)

1 个答案:

答案 0 :(得分:1)

您没有将属性选择字典传递到find_all()函数中。替换:

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

使用:

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

现在你的代码会产生:

17103
17104
相关问题