我一直在努力与美丽的汤和一个网页。我想解析网页中的特定表格,但是我遇到了问题。我的代码如下:使用美丽的汤解析表
# -*- coding: cp1252 -*-
import urllib2
from bs4 import BeautifulSoup
page = urllib2.urlopen("http://www.snet.gob.sv/googlemaps/workstation/main.php").read()
soup = BeautifulSoup(page)
data = []
table = soup.find("table", { "class" : "mytable" })
table_body = table.find('tbody')
rows = table_body.find_all('tr')
for row in rows:
cols = row.find_all('td')
cols = [ele.text.strip() for ele in cols]
data.append([ele for ele in cols if ele]) # Get rid of empty values
print data
它适用于其他网页,但不适用于此。我得到以下错误:
table_body = table.find('tbody')
AttributeError: 'NoneType' object has no attribute 'find'
它似乎没有找到标签“tbody”,但我已检查,它在代码中。另一个问题是,当它工作时(其他网页),表格的每一项旁边都会出现“u”。我搜查了很多,我找不到问题。谢谢你的帮助。
这意味着在这里_is_没有一个mytable类的表。 – Rishav
感谢您的回答@RishavKundu。我检查了网页,并且有一张“mytable”表。 – Ernesto561