문제

I´ve scraped these tables in one single table in python using BeautifulSoup. The code is the following :

import urllib2
from bs4 import BeautifulSoup
for i in range(0,39):
    first=urllib2.urlopen("http://www.admision.unmsm.edu.pe/res20130914/A/011/"+str(i)+".html").read()
    soup=BeautifulSoup(first)
    for tr in soup.find_all('tr')[2:]:
      tds = tr.find_all('td')
      print tds[0].text, tds[1].text, tds[2].text, tds[3].text

The result is something like this :

494560 ABAD SAAVEDRA, GERSON HORACIO 011 1116.8750
455314 ABAD VALVERDE, MARIA ISABEL 011 1482.7500
491005 ABREGU HUAMAN, MERCEDES LILIANA 011 503.4000
457929 ACOSTA ABAD, ALEJANDRO FRANCISCO 011 413.0500

So, how can I export this table to CSV?

도움이 되었습니까?

해결책

Use csv module:

import csv
import urllib2
from bs4 import BeautifulSoup

with open('listing.csv', 'wb') as f:
    writer = csv.writer(f)
    for i in range(39):
        url = "http://www.admision.unmsm.edu.pe/res20130914/A/011/{}.html".format(i)
        u = urllib2.urlopen(url)
        try:
            html = u.read()
        finally:
            u.close()
        soup=BeautifulSoup(html)
        for tr in soup.find_all('tr')[2:]:
            tds = tr.find_all('td')
            row = [elem.text.encode('utf-8') for elem in tds[:4]]
            writer.writerow(row)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top