python读写csv文件的方法

yipeiwu_com6年前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生成指定数量的优惠码实操内容

Python生成指定数量的优惠码实操内容

Python生成指定数量的优惠码 打开Python开发工具IDLE,新建‘codeGen.py'文件,并保存 导入需要的包,这里需要random和string,代码如下: impo...

Python连接mysql数据库的正确姿势

Python连接mysql数据库的正确姿势

Python 数据库接口支持非常多的数据库,你可以选择适合你项目的数据库: GadFly mSQL MySQL PostgreSQL Microsoft SQL S...

详解Python中for循环是如何工作的

详解Python中for循环是如何工作的

前言 for...in 是Python程序员使用最多的语句,for 循环用于迭代容器对象中的元素,这些对象可以是列表、元组、字典、集合、文件,甚至可以是自定义类或者函数,例如: 作用于列...

python使用Apriori算法进行关联性解析

从大规模数据集中寻找物品间的隐含关系被称作关联分析或关联规则学习。过程分为两步:1.提取频繁项集。2.从频繁项集中抽取出关联规则。 频繁项集是指经常出现在一块的物品的集合。 关联规...

在ubuntu16.04中将python3设置为默认的命令写法

在ubuntu16.04中将python3设置为默认的命令写法

直接执行这两个命令即可: sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 100...