蟒蛇BeautifulSoup分析表 [英] python BeautifulSoup parsing table
本文介绍了蟒蛇BeautifulSoup分析表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
伙计们,我学习蟒蛇要求
和BeautifulSoup。对于运动,我选择写一个快速NYC停车票解析器。我能得到一个HTML响应这是相当难看。我需要抢 lineItemsTable
并解析所有的门票。
Folks, I learning python requests
and BeautifulSoup. For the exercise, I've chosen to write a quick NYC parking ticket parser. I am able to get a html response which is quite ugly. I need to grab the lineItemsTable
and parse all the tickets.
您可以去这里重现页板 T630134C
soup = BeautifulSoup(plateRequest.text)
#print(soup.prettify())
#print soup.find_all('tr')
table = soup.find("table", { "class" : "lineItemsTable" })
for row in table.findAll("tr"):
cells = row.findAll("td")
print cells
有人可以帮助我吗?简单的寻找所有的 TR
没有得到我的任何地方。
Can someone please help me out? Simple looking for all tr
does not get me anywhere.
谢谢!
推荐答案
在这里你去:
data = []
table = soup.find('table', attrs={'class':'lineItemsTable'})
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
这给你:
[ [u'1359711259', u'SRF', u'08/05/2013', u'5310 4 AVE', u'K', u'19', u'125.00', u'$'],
[u'7086775850', u'PAS', u'12/14/2013', u'3908 6th Ave', u'K', u'40', u'125.00', u'$'],
[u'7355010165', u'OMT', u'12/14/2013', u'3908 6th Ave', u'K', u'40', u'145.00', u'$'],
[u'4002488755', u'OMT', u'02/12/2014', u'NB 1ST AVE @ E 23RD ST', u'5', u'115.00', u'$'],
[u'7913806837', u'OMT', u'03/03/2014', u'5015 4th Ave', u'K', u'46', u'115.00', u'$'],
[u'5080015366', u'OMT', u'03/10/2014', u'EB 65TH ST @ 16TH AV E', u'7', u'50.00', u'$'],
[u'7208770670', u'OMT', u'04/08/2014', u'333 15th St', u'K', u'70', u'65.00', u'$'],
[u'$0.00\n\n\nPayment Amount:']
]
东西夫妇要注意:
Couple of things to note:
- 在最后一排上面的输出,付款金额不是一部分
表但那是表的布局方式。你可以过滤它
出通过检查列表的长度是小于7。 - 每一行的最后一列将必须分开处理,因为它是一个输入的文本框。
这篇关于蟒蛇BeautifulSoup分析表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文