python BeautifulSoup解析表 [英] python BeautifulSoup parsing table
问题描述
我正在学习 python requests
和 BeautifulSoup.作为练习,我选择编写一个快速的纽约停车罚单解析器.我能够得到一个非常难看的 html 响应.我需要获取 lineItemsTable
并解析所有票证.
I'm learning python requests
and BeautifulSoup. For an exercise, I've chosen to write a quick NYC parking ticket parser. I am able to get an html response which is quite ugly. I need to grab the lineItemsTable
and parse all the tickets.
您可以通过以下方式重现该页面:https://paydirect.link2gov.com/NYCParking-Plate/ItemSearch
并输入 NY
车牌 T630134C
You can reproduce the page by going here: https://paydirect.link2gov.com/NYCParking-Plate/ItemSearch
and entering a NY
plate 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
Payment Amount:']
]
需要注意的几点:
- 上面输出的最后一行,Payment Amount 不是一部分的桌子,但这就是桌子的布置方式.你可以过滤它通过检查列表的长度是否小于 7 来输出.
- 每行的最后一列必须单独处理,因为它是一个输入文本框.
这篇关于python BeautifulSoup解析表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!