Python BeautifulSoup找不到行
我试图从网页中的表格中提取几个单元格,并最终将结果导出到excel。Python BeautifulSoup找不到行,python,selenium,web-scraping,html-table,beautifulsoup,Python,Selenium,Web Scraping,Html Table,Beautifulsoup,我试图从网页中的表格中提取几个单元格,并最终将结果导出到excel。 这些细胞位于id为“fila_88”的T体内。 每一行都包含在一个标记中。 每个单元格都包含在一个标记中。 我有以下python代码: sopa = BeautifulSoup(browser.page_source, 'html.parser') # Creates BS element tabela = sopa.find_all('tbody', id = "fila_88") # Extracts table from
这些细胞位于id为“fila_88”的T体内。
每一行都包含在一个标记中。
每个单元格都包含在一个标记中。
我有以下python代码:
sopa = BeautifulSoup(browser.page_source, 'html.parser') # Creates BS element
tabela = sopa.find_all('tbody', id = "fila_88") # Extracts table from source, this works
linha = tabela.find_all('tr') # This line fails
我得到的错误是:
"ResultSet object has no attribute '%s'. You're probably treating a list of elements like a single element. Did you call find_all() when you meant to call find()?" % key
我的源代码如下所示:
谁能解释一下我做错了什么
编辑:为了清晰起见,这里是提取的表格:你能试试这个吗
tabela = sopa.find('tbody', id = "fila_88")
for linha in tabela.find_all('tr'):
row= linha.find('celltag')
改为:
sopa = BeautifulSoup(browser.page_source, 'html.parser') # Creates BS element
tabela = sopa.find('tbody', {'id':"fila_88"}) # Extracts table from source, this works
linha = tabela.find_all('tr')
也要考虑使用熊猫。它从html返回一个表列表,然后抓取所需的表。例如:
import pandas as pd
dfs = pd.read_html(browser.page_source)
print(dfs[1].to_string())
输出:
print (dfs[1].to_string())
Solic. Abertura Solicitante Serviço Fila Transferência Pri. SLA
0 208293 06/03/2018 12:13 (lcmuniz) Leia Cristiane Loeblein Fernandes Muniz Erros MV Sistemas (customizações) 25/09/2019 21:37 B NaN
1 208615 08/03/2018 14:03 (randreola) Rafaela Andreola Erros MV Sistemas (customizações) 24/09/2019 11:15 A NaN
2 209805 20/03/2018 11:32 (rejane) Rejane Maria Basso Erros MV Sistemas 30/01/2020 10:06 B NaN
3 209891 20/03/2018 17:11 (randreola) Rafaela Andreola Adequações MV Sistemas (customizações) 28/09/2019 19:51 4 NaN
4 210035 22/03/2018 10:03 (rejane) Rejane Maria Basso Erros MV Sistemas 20/12/2018 15:28 B NaN
5 211811 09/04/2018 17:20 (randreola) Rafaela Andreola Erros MV Sistemas 04/09/2019 14:46 B NaN
6 212559 17/04/2018 10:34 (rejane) Rejane Maria Basso Adequações MV Sistemas (customizações) 28/09/2019 21:40 4 NaN
7 216167 23/05/2018 15:16 (lbarros) Loreci Klipel de Barros Adequações MV Sistemas 31/07/2019 11:40 5 NaN
8 219056 27/06/2018 09:11 (rejane) Rejane Maria Basso Cadastro MV Sistemas 30/09/2019 15:34 5 NaN
9 219081 27/06/2018 10:50 (rejane) Rejane Maria Basso Adequações MV Sistemas (customizações) 28/09/2019 22:09 4 NaN
10 219376 29/06/2018 11:44 (randreola) Rafaela Andreola Erros MV Sistemas 31/01/2020 11:23 A NaN
11 222223 02/08/2018 09:49 (randreola) Rafaela Andreola Erros MV Sistemas (customizações) 28/09/2019 22:15 4 NaN
12 223034 10/08/2018 16:27 (rejane) Rejane Maria Basso Erros MV Sistemas (customizações) 14/10/2019 15:27 A NaN
13 224564 29/08/2018 11:40 (fvivian) Fernando Vivian Adequações MV Sistemas (customizações) 25/09/2019 22:45 5 NaN
14 230189 06/11/2018 17:03 (tlaurini) Tarcio Jovani Laurini Adequações MV Sistemas 12/12/2018 16:13 4 NaN
15 230813 12/11/2018 17:11 (cfontoura) Cleonice Fatima Faganello da Fontoura Adequações MV Sistemas (customizações) 28/09/2019 22:31 4 NaN
16 231827 23/11/2018 10:56 (cverdi) Clanir Lurdes Leoncio Verdi Dúvidas MV Sistemas 04/10/2019 16:13 4 NaN
17 232326 29/11/2018 12:50 (vsiqueira) Viviane Oliveira Siqueira Adequações MV Sistemas (customizações) 28/09/2019 22:35 4 NaN
18 232474 30/11/2018 13:57 (rejane) Rejane Maria Basso Adequações MV Sistemas (customizações) 29/09/2019 09:23 4 NaN
19 232496 30/11/2018 15:57 (brunomolina) Bruno Vieira Molina Adequações MV Sistemas (customizações) 25/09/2019 11:51 5 NaN
20 234738 02/01/2019 18:03 (cverdi) Clanir Lurdes Leoncio Verdi Erros MV Sistemas 23/01/2019 09:37 4 NaN
21 236830 23/01/2019 13:56 (mmenegat) Monica Menegat Adequações MV Sistemas (customizações) 29/09/2019 09:55 4 NaN
...
谢谢你的回复!我尝试了你的代码,但它返回相同的错误。也许桌子在某种程度上有问题?这里我只提取了一个表:tabela=sopa.find_all('tbody',id=“fila_88”)将其更改为tabela=sopa.find('tbody',id=“fila_88”)您从错误消息中理解了什么?对我来说,这似乎是相当有见地和直截了当的。