zoukankan      html  css  js  c++  java
  • 团队-爬虫豆瓣top250项目-开发文档

    项目地址:https://gitee.com/Lindom/pachong/blob/master/%E6%96%B0%E5%BB%BA%E6%96%87%E6%9C%AC%E6%96%87%E6%A1%A3%20(2).py

    在本次项目中我担任的是代码整合。目前我们的项目已经初步完成,能够爬取到想要的数据。

    代码各功能已经做了 简单的注释。

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    """
    一个简单的Python爬虫, 用于抓取豆瓣电影Top前100的电影的名称

    """
    import string
    import re
    import urllib2

    class DouBanSpider(object) :
    """类的简要说明

    本类主要用于抓取豆瓣前100的电影名称

    Attributes:
    page: 用于表示当前所处的抓取页面
    cur_url: 用于表示当前争取抓取页面的url
    datas: 存储处理好的抓取到的电影名称
    _top_num: 用于记录当前的top号码
    """

    def __init__(self) :
    self.page = 1
    self.cur_url = "http://movie.douban.com/top250?start={page}&filter=&type="
    self.datas = []
    self._top_num = 1
    print "豆瓣电影爬虫准备就绪, 准备爬取数据..."

    def get_page(self, cur_page) :
    """

    根据当前页码爬取网页HTML

    Args:
    cur_page: 表示当前所抓取的网站页码

    Returns:
    返回抓取到整个页面的HTML(unicode编码)

    Raises:
    URLError:url引发的异常
    """
    url = self.cur_url
    try :
    my_page = urllib2.urlopen(url.format(page = (cur_page - 1) * 25)).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 my_page

    def find_title(self, my_page) :
    """

    通过返回的整个网页HTML, 正则匹配前100的电影名称


    Args:
    my_page: 传入页面的HTML文本用于正则匹配
    """
    temp_data = []
    movie_items = re.findall(r'<span.*?class="title">(.*?)</span>', my_page, re.S)
    for index, item in enumerate(movie_items) :
    if item.find("&nbsp") == -1 :
    temp_data.append("Top" + str(self._top_num) + " " + item)
    self._top_num += 1
    self.datas.extend(temp_data)

    def start_spider(self) :
    """

    爬虫入口, 并控制爬虫抓取页面的范围
    """
    while self.page <= 4 :
    my_page = self.get_page(self.page)
    self.find_title(my_page)
    self.page += 1

    def main() :
    print """
    ###############################
    一个简单的豆瓣电影前100爬虫
    Author: Andrew_liu
    Version: 0.0.1
    Date: 2014-12-04
    ###############################
    """
    my_spider = DouBanSpider()
    my_spider.start_spider()
    for item in my_spider.datas :
    print item
    print "豆瓣爬虫爬取结束..."

    if __name__ == '__main__':
    main()

  • 相关阅读:
    Domain many-to-many
    程序员技术练级攻略
    设置 Eclipse 智能代码提示,大幅度减少 alt+/ 使用频率,打每个字都出现代码提示的办法
    How to Build FFmpeg for Android
    How to Build Android Applications Based on FFmpeg by An Example
    winArchiver(version4.7) 软件测试
    wxWidgets vs开发环境配置
    电脑系统问题定位tips
    hisi3519开发平台配置流程
    amazon alexa使用体验
  • 原文地址:https://www.cnblogs.com/Lindom/p/7780488.html
Copyright © 2011-2022 走看看