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