gpt4 book ai didi

python - 将抓取的表导出到 CSV

转载 作者:行者123 更新时间:2023-11-30 23:29:20 25 4
gpt4 key购买 nike

我已经使用 BeautifulSoup 将这些表抓取到了 python 中的一个表中。代码如下:

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

结果是这样的:

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

那么,如何将该表导出为 CSV?

最佳答案

使用csv模块:

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)

关于python - 将抓取的表导出到 CSV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21213322/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com