練習---這い出した豆弁TOP 250の書籍をcsvファイルに保存する
1639 ワード
xlwtでexcelテーブルに格納すると書いたことがありますが、今回はcsvファイルに格納します.
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('
','')+'》')
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('C:/Users/Xpeng/Desktop/ /top250_books.csv','w',newline='',encoding='utf-8')
w=csv.writer(csv_file)
w.writerow([' ',' ',' ',' '])
for b in books:
w.writerow(b)
csv_file.close()