<分区> 分区>
我使用下面的代码从网站上读取表格。 对于第一个示例,一切都按预期工作。 对于第二个示例(注释变量),我只得到第一列。我找不到原因。有人可以帮忙吗?
创建更好的表格输出的简单方法也不错。
import urllib2
import pprint
from bs4 import BeautifulSoup
URL = 'http://www.proplanta.de/Markt-und-Preis/MATIF-Raps/'
TABLENR = 36
#URL = 'http://www1.chineseshipping.com.cn/en/indices/ccfinew.jsp'
#TABLENR = 4
req = urllib2.Request(URL, headers={'User-Agent' : "My Browser"})
con = urllib2.urlopen( req )
html = con.read()
soup = BeautifulSoup(html)
tables = soup.find_all('table')
data = []
rows = tables[TABLENR].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
pprint.pprint (data)
