您的位置:首页 > 编程语言 > Python开发

Python爬虫入门-爬取豆瓣图书Top25

2018-10-02 15:18 344 查看

代码如下:

from bs4 import BeautifulSoup
import requests
ready_url="https://book.douban.com/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+'\n')+("作者:"+a+'\n')+("评分:"+p+'\n')+("致辞:"+w+'\n')+("\n---------------------\n")
f.write(all)
n=n+25
f.close()
阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: