代码如下:
from bs4 import BeautifulSoup
import requests
ready_url="/top250?start="
#豆瓣把top250的图书放在了10个页面,分别是ready+url+0 25 50 75 100 125 150 175 200 225
headers = {"User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36"}#伪装成浏览器
n=0
f=open("L:/豆瓣图书.txt","w",encoding="utf-8")
while n <=225:
url=ready_url+str(n)
req=requests.get(url,headers=headers)
soup=BeautifulSoup(req.text,"lxml")#以lxml解释器读取下载的网页文本
alldiv=soup.find_all("div",class_="pl2")#alldiv是所有div 下class=pl2的
bookname=[a.find("a")["title"]for a in alldiv]#生成列表:用alldiv的yitle属性值
alldiv=soup.find_all("p",class_="pl")
author=[a.get_text() for a in alldiv]
alldiv=soup.find_all("span",class_="rating_nums")
point=[a.get_text() for a in alldiv]
alldiv=soup.find_all("span",class_="inq")
word=[a.get_text() for a in alldiv]
#这样,该网站就算剽窃成功了,但也可以将这些东西存到一个txt中去
# 前面再开个文件夹,再把书的封面搞下来是最好的
for b,a,p,w in zip(bookname,author,point,word):#用zip整合遍历多个列表
all=("书名:"+b+" ")+("作者:"+a+" ")+("评分:"+p+" ")+("致辞:"+w+" ")+(" --------------------- ")
f.write(all)
n=n+25
f.close()
如果觉得《python爬虫书籍豆瓣评分-Python爬虫入门-爬取豆瓣图书Top25》对你有帮助,请点赞、收藏,并留下你的观点哦!