from bs4 import BeautifulSoup
def commentslistbyindex(index):
ss=requests.get("http://www.aigaogao.com/tools/history.html?s=300357").content
soup = BeautifulSoup(ss,"lxml")
tables = soup.findAll('table')
tab=tables[-1]
price =[]
trs=tab.findAll('tr')
print len(trs)
trs.pop(0)
trs.pop()
for tr in trs:
price.append(float(tr.findAll('td')[4].getText().encode("gb18030")))
avg=sum(price)/len(price)
strpr="%f,%f,%f,%f,%f\n" % (min(price),max(price),price[0],avg,avg/price[0])
print soup.title
print strpr
如果使用 html.parser 参数 得不到 table 表里的真正数据 lxml 兼容性好.