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()阅读更多
相关文章推荐
- 简单的python爬虫爬豆瓣图书TOP250
- [python爬虫入门]爬取豆瓣电影排行榜top250
- [Python爬虫]2.豆瓣图书Top250
- [置顶] python爬虫实践——零基础快速入门(三)爬取豆瓣图书
- python爬虫实现获取豆瓣图书的top250的信息-beautifulsoup实现
- python3[爬虫基础入门实战] 爬取豆瓣电影排行top250
- python 爬虫实战(一)爬取豆瓣图书top250
- Python爬虫一:抓取豆瓣电影Top250
- Python爬虫(二)—— 再探豆瓣Top250
- Python爬虫初学(1)豆瓣电影top250评论数
- 一个简单的python爬虫程序 爬取豆瓣热度Top100以内的电影信息
- 爬虫入门:python+pycharm,豆瓣电影信息,短评,分页爬取,mysql数据库连接
- Python爬虫入门 | 7 分类爬取豆瓣电影,解决动态加载问题
- [python爬虫] BeautifulSoup和Selenium对比爬取豆瓣Top250电影信息
- Python爬虫(三)——对豆瓣图书各模块评论数与评分图形化分析
- Python数据分析----Python3操作Excel-以豆瓣图书Top250为例
- python3 [入门基础实战] 爬虫入门之爬取豆瓣阅读中文电子书[热门排序]
- Python爬虫豆瓣电影top250
- Python爬虫实战——豆瓣电影Top250
- python正则表达式简单爬虫入门+案例(爬取猫眼电影TOP榜)