from urllib.request import urlopen |
from bs4 import BeautifulSoup |
import csv |
html = urlopen( 'https://en.wikipedia.org/wiki/Comparison_of_text_editors' ) |
bsObj = BeautifulSoup(html, 'html.parser' ) |
# 住对比表格是当前页的第一个表格 |
table = bsObj.findAll( 'table' ,{ 'class' : 'wikitable sortable' })[ 0 ] # 0表示第一个table |
rows = table.findAll( 'tr' ) |
csvFile = open ( 'G:\\editor.csv' , 'wt' ,newline = ' ',encoding = ' utf - 8 ') # t指以文本模式打开 |
writer = csv.writer(csvFile) |
try : |
for row in rows: |
csvRow = [] |
for cell in row.findAll([ "td" , "th" ]): # 迭代出当前tr下的所有td,th |
csvRow.append(cell.get_text()) |
writer.writerow(csvRow) # 将整个tr中的th列表以横向写入文件 |
finally : |
csvFile.close() |