我一直在努力與美麗的湯和一個網頁。我想解析網頁中的特定表格,但是我遇到了問題。我的代碼如下:使用美麗的湯解析表
# -*- 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