2010-02-08 3 views
7

Ich muss die Daten aus der Tabelle in Textdatei (output.txt) in diesem Format: data1; Daten2; data3; data4; .....Parsingtabelle mit BeautifulSoup und schreiben in Textdatei

Celková podlahova plocha bytu; 33m; Vytah; Ano; Nadzemne podlazie; Prizemne podlazie; .....; Forma vlastníctva; osobne

Alle in "eine Zeile" Separator ";" (später Export in csv- Datei).

Ich bin Anfänger .. Hilfe, danke.

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() 

Antwort

11

Sie halten nicht jeden Datensatz, wie Sie es in Versuchen Sie dies lesen, die die Datensätze in records speichert.

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() 

Diese mehr gereinigt werden können, aber ich denke, es ist, was du bist wollen.

0

hier ist eine Alternative nicht BS Art und Weise, nur für Ihre Aufgabe

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) 

Ausgang

$ ./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

Sollte sein ';'. Join (Speicher) als Semikolons zwischen den einzelnen Posten erforderlich sind. – pwdyson

+0

Wow, das ist großartig - aber Sie haben eine Grenze darin, nur den ersten Gegenstand zu greifen. Wie kann man weiterhin ALLE Daten in der Tabelle erfassen, einschließlich verschachtelter Tabellen? – itsricky