我想將表數據轉換爲CSV文件。不幸的是,我遇到了一個障礙,下面的代碼簡單地重複從所有後續TR中的第一個TR開始的TD。Python beautifulsoup迭代表
import urllib.request
from bs4 import BeautifulSoup
f = open('out.txt','w')
url = "http://www.international.gc.ca/about-a_propos/atip-aiprp/reports-rapports/2012/02-atip_aiprp.aspx"
page = urllib.request.urlopen(url)
soup = BeautifulSoup(page)
soup.unicode
table1 = soup.find("table", border=1)
table2 = soup.find('tbody')
table3 = soup.find_all('tr')
for td in table3:
rn = soup.find_all("td")[0].get_text()
sr = soup.find_all("td")[1].get_text()
d = soup.find_all("td")[2].get_text()
n = soup.find_all("td")[3].get_text()
print(rn + "," + sr + "," + d + ",", file=f)
這是我的第一個Python腳本,所以任何幫助將不勝感激!我已經看過其他問題的答案,但無法弄清楚我在這裏做錯了什麼。