python爬虫 豆瓣电影
2015-08-03 17:57
861 查看
最近学习python 顺便写下爬虫练手
爬的是豆瓣电影排行榜 http://movie.douban.com/chart
python版本2.7.6
下面是py代码
用面向对象改了下 加入时间
效果图:
参考网站:
http://beautifulsoup.readthedocs.org/zh_CN/latest/
http://cuiqingcai.com/1319.html
爬的是豆瓣电影排行榜 http://movie.douban.com/chart
python版本2.7.6
// 安装 Beautiful Soup sudo apt-get install Python-bs4 // 安装 requests sudo apt-get install Python-requests
下面是py代码
__author__ = 'wzz' # -*- coding: utf-8 -*- import requests from bs4 import BeautifulSoup r = requests.get('http://movie.douban.com/chart') page_code = BeautifulSoup(r.text) # 查找clss为indent里面的table标签 items = page_code.select('.indent table') line = '-----------------------------------' # 根据评分显示星星数量 def getStar(num): str = '' for i in range(0, 5): # 等价于 num/2>i?'★':'☆' str += '★'if int(float(num))/2>i else'☆' return str for item in items: # 查找clss为pl2的div里面的a标签 name = item.select('div.pl2 a')[0] # 删除span标签 name.span.unwrap() # 去掉空格和换行 name = name.text.replace(' ', '').replace('\n', '') actor = item.select('div.pl2 p.pl')[0] actor = actor.text score = item.select('span.rating_nums')[0] score = score.text num = item.select('span.pl')[0] num = num.text print '\n%s\n%s\n%s\n%s' %(name, line, actor, line) # ★特殊字符不能用 %s 来输出 有没有知道的跟我说一下 print getStar(score), score, num
用面向对象改了下 加入时间
__author__ = 'wzz' # -*- coding: utf-8 -*- import requests import time from bs4 import BeautifulSoup # 豆瓣电影爬虫 class doubanMove: def __init__(self): self.url = 'http://movie.douban.com/chart' self.line = '-----------------------------------' # 根据url获得页面并提取数组 def getPageItem(self): r = requests.get(self.url) page_code = BeautifulSoup(r.text) items = page_code.select('.indent table') return items # 根据评分显示星星数量 def getStar(self, num): str = '' for i in range(0, 5): # 等价于 num/2>i?'★':'☆' str += '★'if int(float(num))/2>i else'☆' return str def start(self): print '加载中...' startTime = time.time() items = self.getPageItem() print '用时:', time.time()-startTime, '秒' for item in items: # 查找clss为pl2的div里面的a标签 name = item.select('div.pl2 a')[0] # 删除span标签 name.span.unwrap() # 去掉空格和换行 name = name.text.replace(' ', '').replace('\n', '') actor = item.select('div.pl2 p.pl')[0] actor = actor.text score = item.select('span.rating_nums')[0] score = score.text num = item.select('span.pl')[0] num = num.text print '\n%s\n%s\n%s\n%s' %(name, self.line, actor, self.line) # ★特殊字符不能用 %s 来输出 有没有知道的跟我说一下 print self.getStar(score), score, num dbMove = doubanMove() dbMove.start()
效果图:
参考网站:
http://beautifulsoup.readthedocs.org/zh_CN/latest/
http://cuiqingcai.com/1319.html
相关文章推荐
- 合并百度影音的离线数据 with python 2.3 格式更新
- python_学习笔记0803
- python操作Excel读写--使用xlrd
- Python中将(字典,列表等)变量格式化输出
- python读取xml文件
- python读取xml文件
- Python's Coding Style
- QGIS python开发手册
- [python]小练习__创建你自己的命令行 地址簿 程序
- python 操作文件和目录
- 在windows中安装Python
- python StringIO和BytesIO
- python StringIO和BytesIO
- python IO编程
- Python中wx.FlexGridSizer
- Python中super关键字的作用
- 深刻理解Python中的元类(metaclass)
- python中try except处理程序异常的三种常用方法
- win7下安装scrapy(开源python爬虫)
- python正则表达式函数match()和search()的区别