python爬虫(豆瓣新片榜)
2015-03-23 16:22
190 查看
#!/usr/bin/env python # coding: utf-8 import re import urllib2 class doubanTop10: def __init__(self): self.url = 'http://movie.douban.com/chart' self.datas = [] self._top_num = 1 print "正在爬取豆瓣新片榜...\n" def get_data(self, url): url = self.url try: page_data = urllib2.urlopen(url).read().decode('utf-8') except urllib2.URLError, e: if hasattr(e, 'code'): print "The server couldn't fulfill the request." print "Error code: %s" % e.code elif hasattr(e, 'reason'): print "We failed to reach a server. Please check your url and read the Reason." print "Reason: %s" % e.reason return page_data def find_title(self, page_data): temp_data = [] # print page_data movie_items = re.findall(r'<a.*?class="nbg".*?title="(.*?)">', page_data, re.S) for index, item in enumerate(movie_items): if item.find(" ") == -1: temp_data.append("Top" + str(self._top_num) + " " + item) self._top_num += 1 self.datas.extend(temp_data) def start_spider(self): my_page = self.get_data(self.url) self.find_title(my_page) def main(): spider = doubanTop10() spider.start_spider() for item in spider.datas: print item print "\n爬取完成!" if __name__ == '__main__': main()
相关文章推荐
- python 爬虫,抓取豆瓣勾搭组妹纸照片
- Python爬虫初学(2)豆瓣电影top250评论数
- 实践Python的爬虫框架Scrapy来抓取豆瓣电影TOP250
- python3 爬虫 模拟登陆豆瓣修改签名
- 用Python爬虫爬取豆瓣电影、读书Top250并排序
- Python 爬虫抓站 记录(虾米,百度,豆瓣,新浪微博)
- python爬虫入门笔记:用scrapy爬豆瓣
- Python爬虫学习笔记——豆瓣登陆(三)
- Python爬虫学习笔记——豆瓣登陆(二)
- (8)Python爬虫——爬取豆瓣影评数据
- Python豆瓣爬虫,指定文件行数写入到文件中
- Python爬虫之模拟登录豆瓣获取最近看过的电影
- 用python分布式地爬虫豆瓣/Twitter
- Python爬虫学习笔记——防豆瓣反爬虫
- Python—爬虫—简单爬取豆瓣电影
- Python爬虫初学(1)豆瓣电影top250评论数
- 基于python的豆瓣“我看过的电影”的爬虫
- Python爬虫学习记录(0)——Python 爬虫抓站 记录(虾米,百度,豆瓣,新浪微博)
- 编写Python爬虫抓取豆瓣电影TOP100及用户头像的方法
- python爬虫(爬取豆瓣电影)_动态网页,json解释,中文编码