9.python3實用編程技巧進階(四)

4.1.如何讀寫csv數據

 爬取豆瓣top250書籍html

import requests
import json
import csv
from bs4 import BeautifulSoup

books = []
def book_name(url): res = requests.get(url) html = res.text soup = BeautifulSoup(html, 'html.parser') items = soup.find(class_="grid-16-8 clearfix").find(class_="indent").find_all('table') for i in items: book = [] title = i.find(class_="pl2").find('a') book.append('' + title.text.replace(' ', '').replace('\n', '') + '') star = i.find(class_="star clearfix").find(class_="rating_nums") book.append(star.text + '') try: brief = i.find(class_="quote").find(class_="inq") except AttributeError: book.append('」暫無簡介「') else: book.append(brief.text) link = i.find(class_="pl2").find('a')['href'] book.append(link) global books books.append(book) print(book) try: next = soup.find(class_="paginator").find(class_="next").find('a')['href'] # 翻到最後一頁 except TypeError: return 0 else: return next next = 'https://book.douban.com/top250?start=0&filter=' count = 0 while next != 0: count += 1 next = book_name(next) print('-----------以上是第' + str(count) + '頁的內容-----------') csv_file = open('D:/top250_books.csv', 'w', newline='', encoding='utf-8') w = csv.writer(csv_file) w.writerow(['書名', '評分', '簡介', '連接']) for b in books: w.writerow(b)

結果json

把評分爲9.0的書籍保存到book_out.csv文件中app

'''
1.爬取豆瓣評分排行前250本書,保存爲top250.csv
2.讀取top250.csv文件,把評分爲9.0以上的書籍保存到另一個csv文件中
'''

import csv

#打開的時候必須用encoding='utf-8',不然報錯
with open('top250_books.csv', encoding='utf-8') as rf:
    reader = csv.reader(rf)
    #讀取頭部
    headers = next(reader)
    with open('books_out.csv', 'w', encoding='utf-8') as wf:
        writer = csv.writer(wf)
        #把頭部信息寫進去
        writer.writerow(headers)

        for book in reader:
            #獲取評分
            score = book[1]
            #把評分大於9.0的過濾出來
            if score and float(score) >= 9.0:
                writer.writerow(book)

 4.2.如何讀寫excel

 安裝兩個庫url

pip install xlrd xlwt

讀取excelspa

#4.2.如何讀取excel

import xlrd

book = xlrd.open_workbook('demo.xlsx')

sheet = book.sheet_by_index(0)
#獲取有多少行多少列
print(sheet.nrows)   #4
print(sheet.ncols)   #4

print(sheet.cell(0,0))          #text:'姓名'
print(sheet.cell_value(0,0))    #姓名

print(sheet.row_values(0))      #['姓名', '語文', '數學', '外語']
print(sheet.row_values(1,1))    #[95.0, 99.0, 96.0]

求分數的總和excel

#4.2.如何讀寫excel

import xlrd, xlwt

rbook = xlrd.open_workbook('demo.xlsx')
rsheet = rbook.sheet_by_index(0)

k = rsheet.ncols
#在最後添加一列 ‘總分’
rsheet.put_cell(0,k,xlrd.XL_CELL_TEXT, '總分', None)

for i in range(1,rsheet.nrows):
    #求分數總和
    t = sum(rsheet.row_values(i, 1))
    rsheet.put_cell(i,k,xlrd.XL_CELL_NUMBER,t,None)

wbook = xlwt.Workbook()
wsheet = wbook.add_sheet(rsheet.name)

for i in range(rsheet.nrows):
    for j in range(rsheet.ncols):
        wsheet.write(i,j,rsheet.cell_value(i,j))

wbook.save('out.xlsx')

 結果code

相關文章
相關標籤/搜索