Turn a list into a python pandas csv

Asked

Viewed 147 times

-2

I have the following code that scraps data from an auction site:

lista = []
for pagina in range(1,34):
    url = 'https://www.lance24h.com.br/DetalhesLances.php?Codigo=45919&Pagina={0}'.format(pagina)
    response = requests.get(url)
    html = response.content
    soup = BeautifulSoup(html,'lxml')
    lista = []
    for corpo in soup.find_all('tr'):
      lista.append(corpo.text.strip())

novo = []
for x in lista:
    item = x
    for y in ['\n', '\t','\r', '/', '.', '-', '(', ')']:
        item = item.replace(y, "")
    novo.append(item)

So with feedback I take the following:

['DataValorUsuário',
 '13012021 22:03:02                    R$ 0,59                    Daniel de Santi',
 '13012021 22:03:02                    R$ 0,58                    lordschug',
 '13012021 22:02:50                    R$ 0,57                    socialdomarcos',
 '13012021 22:02:48                    R$ 0,56                    deiaxabs',
 '13012021 22:02:47                    R$ 0,55                    Daniel de Santi',
 '13012021 22:02:35                    R$ 0,54                    lordschug',
 '13012021 22:02:33                    R$ 0,53                    Daniel de Santi',
 '13012021 22:02:27                    R$ 0,52                    socialdomarcos',
 '13012021 22:02:23                    R$ 0,51                    Daniel de Santi',
 '13012021 22:02:11                    R$ 0,50                    lordschug',
 '13012021 22:02:04                    R$ 0,49                    socialdomarcos',
 '13012021 22:02:03                    R$ 0,48                    lordschug',
 '13012021 22:01:54                    R$ 0,47                    Daniel de Santi',
 '13012021 22:01:53                    R$ 0,46                    socialdomarcos',
 '13012021 22:01:42                    R$ 0,45                    lordschug',
 '13012021 22:01:32                    R$ 0,44                    socialdomarcos',
 '13012021 22:01:23                    R$ 0,43                    lordschug',...,]

Now I’m trying to turn into a csv file that has three columns one for the other date for the bid and the last for the bidder’s name. I’ve tried the following codes:

with open("lances.csv", "w", newline="") as f:
    writer = csv.writer(f)
    writer.writerows(lista)

And I got the following return

D,a,t,a,"
",V,a,l,o,r,"
",U,s,u,á,r,i,o
1,3,/,0,1,/,2,0,2,1, ,2,2,:,0,3,:,0,2, , , , , , , , ,"
","
","
", , , , , , , , , , , , ,R,$, ,0,",",5,9, , , , , , , , ,"
","
","
", , , , , , , , , , , , ,D,a,n,i,e,l, ,d,e, ,S,a,n,t,i
1,3,/,0,1,/,2,0,2,1, ,2,2,:,0,3,:,0,2, , , , , , , , ,"
","
","
", , , , , , , , , , , , ,R,$, ,0,",",5,8, , , , , , , , ,"
","
","
", , , , , , , , , , , , ,l,o,r,d,s,c,h,u,g
1,3,/,0,1,/,2,0,2,1, ,2,2,:,0,2,:,5,0, , , , , , , , ,"
","
","
", , , , , , , , , , , , ,R,$, ,0,",",5,7, , , , , , , , ,"
","
","
", , , , , , , , , , , , ,s,o,c,i,a,l,d,o,m,a,r,c,o,s
1,3,/,0,1,/,2,0,2,1, ,2,2,:,0,2,:,4,8, , , , , , , , ,"
","
","

I believe the problem is when I format the list to remove unwanted characters, but I don’t know how to fix it.

1 answer

0


Set the parameters in the writing that will format the way you want

arquivo = open('novoarquivo.csv', 'w', newline='')
#parametros de acordo com os caracteres de escape
escrever= csv.writer(arquivo, delimiter='\t', lineterminator='\n\n') 
escrever.writerow(list(range(1,20)) ) #exemplo de escrita
arquivo.close()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.