HTML テーブルを 2d python リスト (リストのリスト) に変換しようとしています。「列」のうちの 3 つは、対応する HTML テーブル セルのテキストであり、正常に機能します。ただし、1 つの「列」は、対応する HTML セル内のリンクの ID にする必要がありますが、その属性にアクセスできません。
リンクのIDを取得しようとしているときに問題が発生します。その要素の .contents を出力すると、「アクション」しか表示されません。その要素の ['id'] インデックスにアクセスしようとすると、エラーが発生します。なにが問題ですか?
bs = BeautifulSoup(page)
table = bs.find("table", id="ctl00_ContentPlaceHolder1_Name_Reports1_TabContainer1_TabPanel1_dgReports")
def notHeader(css_class):
return css_class is not "gridviewheader"
rows = table.find_all("tr", class_=notHeader)
result = []
for x in range(0, len(rows)):
allcols = rows[x].findAll('td')
tempRow = []
print(allcols[0].contents[0]) #only prints Action
tempRow.append(allcols[0].contents[0]['id']) #TypeError: string indices must be integers
tempRow.append(allcols[2].string)
tempRow.append(allcols[3].string)
tempRow.append(allcols[5].string)
amended = -1
for existing in result:
if tempRow[1] == existing[1] and tempRow[2] == existing[2]:
amended = 1
if amended == -1:
result.append(tempRow)
print (ids)