如何在 table csv 美丽汤中转换打印
How convert print in table csv beautiful soup
我的代码的问题是没有保存在 csv 存档中,创建了一个 csv 但空白。使用打印功能显示结果但不保存在 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])
几点建议。
- 在第 4 行,我希望你放 "html" 只是为了演示,因为你需要一个 url
- 尝试将第 13 行放在第 7 行之前,以防止多个文件访问,这可能会导致错误。
如果你能给出你正在使用的url,我会尝试并给出更好的解决方案。
我得到的csv文件是:
Hora,Nombre
" Alaska y Segura ", 23:50
23:15
原因是每次要写入文件时,您都以 w
模式打开文件。 w
模式替换文件的内容(如果它已经存在) - 它截断文件,它不附加到文件。要追加,您应该使用 a
代替:
f = csv.writer(open("prueba.csv", "a"))
另一个更好的选择是只打开文件一次:
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])
open
函数参见 documentation:
'w' for writing (truncating the file if it already exists), and 'a' for appending
我的代码的问题是没有保存在 csv 存档中,创建了一个 csv 但空白。使用打印功能显示结果但不保存在 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])
几点建议。
- 在第 4 行,我希望你放 "html" 只是为了演示,因为你需要一个 url
- 尝试将第 13 行放在第 7 行之前,以防止多个文件访问,这可能会导致错误。
如果你能给出你正在使用的url,我会尝试并给出更好的解决方案。
我得到的csv文件是:
Hora,Nombre
" Alaska y Segura ", 23:50
23:15
原因是每次要写入文件时,您都以 w
模式打开文件。 w
模式替换文件的内容(如果它已经存在) - 它截断文件,它不附加到文件。要追加,您应该使用 a
代替:
f = csv.writer(open("prueba.csv", "a"))
另一个更好的选择是只打开文件一次:
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])
open
函数参见 documentation:
'w' for writing (truncating the file if it already exists), and 'a' for appending