简体   繁体   中英

How convert print in table csv beautiful soup

the problem of my code is not save in the csv archive, create a csv but in blank. Using the print function show the results but not save in csv.

import csv
import urllib2
from bs4 import BeautifulSoup
url = "html"  
page = urllib2.urlopen(url).read()
soup = BeautifulSoup(page)
for tr in soup.find_all('tr')[2:]:
    tds = tr.find_all('h2') 
    td2 = tr.find_all('th')
    hora = tds[0].text.encode('utf-8')
    nombre = td2[0].text.encode('utf-8')
    print hora, nombre
    f = csv.writer(open("prueba.csv", "w"))
    f.writerow(["Hora", "Nombre"])
    f.writerow([hora, nombre])
1. import csv
2. import urllib2
3. from bs4 import BeautifulSoup
4. url = "html"  
5. page = urllib2.urlopen(url).read()
6. soup = BeautifulSoup(page)
7. for tr in soup.find_all('tr')[2:]:
8.     tds = tr.find_all('h2') 
9.     td2 = tr.find_all('th')
10.    hora = tds[0].text.encode('utf-8')
11.    nombre = td2[0].text.encode('utf-8')
12.    print hora, nombre
13.    f = csv.writer(open("prueba.csv", "w"))
14.    f.writerow(["Hora", "Nombre"])
15.    f.writerow([hora, nombre])

A few suggestions.

  1. in line 4, i hope you put "html" just for demo, as you need a url there
  2. try putting line 13 before line 7 to prevent multiple file access, which may lead to error.

If you could give the url you are using, I'll try out and give better solutions.

The csv file that I get is:

Hora,Nombre

" Alaska y Segura ", 23:50

23:15

The reason is that you open the file in w mode every time you want to write to it. w mode replaces the content of a file if it already exists - it truncates the file, it does not append to the file. To append you should use a instead:

f = csv.writer(open("prueba.csv", "a"))

Another option, which is better since there is no need to close the file and reopen it again and again, is to only open the file once:

import csv
import urllib2
from bs4 import BeautifulSoup
url = r"http://laguiatv.abc.es/programacion/tve-1-807.html"  
page = urllib2.urlopen(url).read()
soup = BeautifulSoup(page)
f = csv.writer(open("prueba.csv", "w"))
for tr in soup.find_all('tr')[2:]:
    tds = tr.find_all('h2') 
    td2 = tr.find_all('th')
    hora = tds[0].text.encode('utf-8')
    nombre = td2[0].text.encode('utf-8')
    print hora, nombre
    f.writerow(["Hora", "Nombre"])
    f.writerow([hora, nombre])

See the documentation for the open function:

'w' for writing (truncating the file if it already exists), and 'a' for appending

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM