Cma*_*mag 70 python beautifulsoup
我正在学习python requests
和BeautifulSoup.对于练习,我选择写一个快速的纽约市停车票解析器.我能得到一个非常难看的HTML回复.我需要抓住lineItemsTable
并解析所有的门票.
你可以通过这里重现页面:https://paydirect.link2gov.com/NYCParking-Plate/ItemSearch
并进入一个NY
盘子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
Run Code Online (Sandbox Code Playgroud)
有人可以帮帮我吗?简单的寻找所有tr
不会让我在任何地方.
sha*_*aan 138
干得好:
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
Run Code Online (Sandbox Code Playgroud)
这给你:
[ [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:']
]
Run Code Online (Sandbox Code Playgroud)
有几点需要注意:
Pou*_*del 40
如果程序员只对解析网页中的表格感兴趣,他们可以使用 pandas 方法pandas.read_html
。
假设我们要从网站中提取 GDP 数据表:https : //worldpopulationreview.com/countries/countries-by-gdp/#worldCountries
然后下面的代码完美地完成了这项工作(不需要beautifulsoup和花哨的html):
import pandas as pd
import requests
url = "https://worldpopulationreview.com/countries/countries-by-gdp/#worldCountries"
r = requests.get(url)
df_list = pd.read_html(r.text) # this parses all the tables in webpages to a list
df = df_list[0]
df.head()
Run Code Online (Sandbox Code Playgroud)
Cma*_*mag 22
解决了,这就是你解析他们的html结果:
table = soup.find("table", { "class" : "lineItemsTable" })
for row in table.findAll("tr"):
cells = row.findAll("td")
if len(cells) == 9:
summons = cells[1].find(text=True)
plateType = cells[2].find(text=True)
vDate = cells[3].find(text=True)
location = cells[4].find(text=True)
borough = cells[5].find(text=True)
vCode = cells[6].find(text=True)
amount = cells[7].find(text=True)
print amount
Run Code Online (Sandbox Code Playgroud)
这是一个通用的工作示例<table>
。(问题链接已断开)
按 GDP(国内生产总值)从此处提取国家/地区的表格。
htmltable = soup.find('table', { 'class' : 'table table-striped' })
# where the dictionary specify unique attributes for the 'table' tag
Run Code Online (Sandbox Code Playgroud)
该tableDataText
函数解析以标签<table>
开头的 html 段,后跟多个<tr>
(表格行)和内部<td>
(表格数据)标签。它返回带有内部列的行列表。<th>
第一行只接受一个(表头/数据)。
def tableDataText(table):
rows = []
trs = table.find_all('tr')
headerow = [td.get_text(strip=True) for td in trs[0].find_all('th')] # header row
if headerow: # if there is a header row include first
rows.append(headerow)
trs = trs[1:]
for tr in trs: # for every table row
rows.append([td.get_text(strip=True) for td in tr.find_all('td')]) # data row
return rows
Run Code Online (Sandbox Code Playgroud)
使用它我们得到(前两行)。
list_table = tableDataText(htmltable)
list_table[:2]
[['Rank',
'Name',
"GDP (IMF '19)",
"GDP (UN '16)",
'GDP Per Capita',
'2019 Population'],
['1',
'United States',
'21.41 trillion',
'18.62 trillion',
'$65,064',
'329,064,917']]
Run Code Online (Sandbox Code Playgroud)
这可以轻松转换pandas.DataFrame
为更高级的工具。
import pandas as pd
dftable = pd.DataFrame(list_table[1:], columns=list_table[0])
dftable.head(4)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
119764 次 |
最近记录: |