2010-02-08 12 views
7

ho bisogno di dati dalla tabella nel file di testo (output.txt) in questo formato: data1; data2; dati3; data4; .....tavolo analisi con BeautifulSoup e scrivere in file di testo

Celková podlahova plocha bytů; 33m; Vytah; Ano; Nadzemne podlazie; Prizemne podlazie; .....; Forma vlastnictva; osobne

All in "una linea", separatore è ";" (più tardi l'esportazione in CSV file).

Sono un principiante .. Aiuto, grazie.

from BeautifulSoup import BeautifulSoup 
import urllib2 
import codecs 

response = urllib2.urlopen('http://www.reality.sk/zakazka/0747-003578/predaj/1-izb-byt/kosice-mestska-cast-sever-sladkovicova-kosice-sever/art-real-1-izb-byt-sladkovicova-ul-kosice-sever') 
html = response.read() 
soup = BeautifulSoup(html) 

tabulka = soup.find("table", {"class" : "detail-char"}) 

for row in tabulka.findAll('tr'): 
    col = row.findAll('td') 
    prvy = col[0].string.strip() 
    druhy = col[1].string.strip() 
    record = ([prvy], [druhy]) 

fl = codecs.open('output.txt', 'wb', 'utf8') 
for rec in record: 
    line = '' 
    for val in rec: 
     line += val + u';' 
    fl.write(line + u'\r\n') 
fl.close() 

risposta

11

Non stanno mantenendo ogni record, come si legge in Prova questo, che memorizza i record in records:.

from BeautifulSoup import BeautifulSoup 
import urllib2 
import codecs 

response = urllib2.urlopen('http://www.reality.sk/zakazka/0747-003578/predaj/1-izb-byt/kosice-mestska-cast-sever-sladkovicova-kosice-sever/art-real-1-izb-byt-sladkovicova-ul-kosice-sever') 
html = response.read() 
soup = BeautifulSoup(html) 

tabulka = soup.find("table", {"class" : "detail-char"}) 

records = [] # store all of the records in this list 
for row in tabulka.findAll('tr'): 
    col = row.findAll('td') 
    prvy = col[0].string.strip() 
    druhy = col[1].string.strip() 
    record = '%s;%s' % (prvy, druhy) # store the record with a ';' between prvy and druhy 
    records.append(record) 

fl = codecs.open('output.txt', 'wb', 'utf8') 
line = ';'.join(records) 
fl.write(line + u'\r\n') 
fl.close() 

Questo potrebbe essere ripulito di più, ma penso che sia quello che sei volendo.

0

ecco un modo alternativo non BS, solo per il vostro compito

store=[] #to store your results 
url="""http://www.reality.sk/zakazka/0747-003578/predaj/1-izb-byt/kosice-mestska-cast-sever-sladkovicova-kosice-sever/art-real-1-izb-byt-sladkovicova-ul-kosice-sever""" 
page=urllib2.urlopen(url) 
data=page.read() 
for table in data.split("</table>"): 
    if "<table" in table and 'class="detail-char' in table: 
     for item in table.split("</td>"): 
       if "<td" in item: 
        store.append(item.split(">")[-1].strip()) 
print ','.join(store) 

uscita

$ ./python.py 
Celková podlahová plocha bytu,33 m2,Výťah,Áno,Nadzemné podlažie,Prízemné podlažie,Stav,Čiastočná rekonštrukcia,Konštrukcia bytu,tehlová,Forma vlastníctva,osobné 
+0

Dovrebbe essere ';'. Join (negozio) come sono necessari punti e virgola tra le voci. – pwdyson

+0

Wow, è grandioso, ma hai un limite in là solo per afferrare il primo oggetto. Come si può continuare ad afferrare TUTTI i dati nella tabella, comprese le tabelle nidificate? – itsricky

Problemi correlati