python讀寫csv文件的方法
更新時間:2019年08月13日 08:37:16 作者:zhang_derek
這篇文章主要介紹了python讀寫csv文件的方法,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值 ,需要的朋友可以參考下
1.爬取豆瓣top250書籍
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) + '頁的內(nèi)容-----------') 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)
結(jié)果
2.把評分為9.0的書籍保存到book_out.csv文件中
''' 1.爬取豆瓣評分排行前250本書,保存為top250.csv 2.讀取top250.csv文件,把評分為9.0以上的書籍保存到另外一個csv文件中 ''' import csv #打開的時候必須用encoding='utf-8',否則報錯 with open('top250.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)
總結(jié)
以上所述是小編給大家介紹的python讀寫csv文件的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
相關文章
python字典setdefault方法和get方法使用實例
這篇文章主要介紹了python字典setdefault方法和get方法使用實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-12-12Django將默認的SQLite更換為MySQL的實現(xiàn)
今天小編就為大家分享一篇Django將默認的SQLite更換為MySQL的實現(xiàn),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11