J'ai fait un web-grattage, et je reçois une table que je veux écrire en CSV.Python web-scraping dans csv
Lorsque je tente, je reçois ce message:
"Traceback (most recent call last):
File "C:/Python27/megoldas3.py", line 27, in <module>
file.write(bytes(header,encoding="ascii",errors="ignore")) TypeError:
str() takes at most 1 argument (3 given)"
Quel est le problème avec ce code? J'utilise Python 2.7.13.
import urllib2
from bs4 import BeautifulSoup
import csv
import os
out=open("proba.csv","rb")
data=csv.reader(out)
def make_soup(url):
thepage = urllib2.urlopen(url)
soupdata = BeautifulSoup(thepage, "html.parser")
return soupdata
maindatatable=""
soup = make_soup("https://www.mnb.hu/arfolyamok")
for record in soup.findAll('tr'):
datatable=""
for data in record.findAll('td'):
datatable=datatable+","+data.text
maindatatable = maindatatable + "\n" + datatable[1:]
header = "Penznem,Devizanev,Egyseg,Penznemforintban"
print maindatatable
file = open(os.path.expanduser("proba.csv"),"wb")
file.write(bytes(header,encoding="ascii",errors="ignore"))
file.write(bytes(maindatatable,encoding="ascii",errors="ignore"))
'bytes' prendra un seul paramètre – Arun
je comprends le message d'erreur, mais je ne sais pas comment je dois corriger. – tardos93
'encoding =" ascii ", errors =" ignorer "' sont les attributs si écrire la fonction pas de bytes, j'ai posté la réponse ci-dessous – Arun