python读写csv文件的方法

yipeiwu_com5年前Python基础

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) + '页的内容-----------')
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)

结果

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)

总结

以上所述是小编给大家介绍的python读写csv文件的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对【听图阁-专注于Python设计】网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

相关文章

Python离线安装PIL 模块的方法

Python离线安装PIL 模块的方法

python的库一般都用pip安装。 但是有时候也会出现在线安装失败的情况,如下图安装PIL模块时报错: 这时候可以采取离线安装的方式; 一、首先下载离线安装包 PIL官方版不支持py...

VSCode下配置python调试运行环境的方法

VSCode下配置python调试运行环境的方法

VSCode配置python调试环境 很久之前的一个东东,翻出来看看 VSCode配置python调试环境 * 1.下载python解释器 * 2.在V...

Django contenttypes 框架详解(小结)

Django contenttypes 框架详解(小结)

一、什么是Django ContentTypes? Django ContentTypes是由Django框架提供的一个核心功能,它对当前项目中所有基于Django驱动的model提供了...

Pandas删除数据的几种情况(小结)

开始之前,pandas中DataFrame删除对象可能存在几种情况 1、删除具体列 2、删除具体行 3、删除包含某些数值的行或者列 4、删除包含某些字符、文字的行或者列 本文就针对这...

Python Web框架Flask信号机制(signals)介绍

信号(signals) Flask信号(signals, or event hooking)允许特定的发送端通知订阅者发生了什么(既然知道发生了什么,那我们可以知道接下来该做什么了)。...