Coder Social home page Coder Social logo

webwalker's Introduction

网站数据抓取二次开发框架

基于scrapy的二次开发框架,通过简单配置,即可实现一个网站分类中所有项目指定信息的抓取
常驻式进程,启动之后,通过feed投放任务,使用redis实现分布式,多台机器多个爬虫实时监控任务抓取

需要掌握技能

  • xpath表达式,正则表达式,以及css表达式,至少会其中一项
  • python 字典和列表数据结构

以下技能最好掌握

  • python lambda 表达式的使用
  • python 简单函数编写
  • 了解scrapy的基本概念,参见scrapy简单介绍

INSTALL

ubuntu && windows

需要python2.7

git clone https://github.com/ShichaoMa/webWalker.git
cd webWalker/walker && (sudo) python setup.py install

or

(sudo) pip install web-walker

HELLOWORLD

1 安装完毕后(推荐pip安装)使用scrapy生成一个项目

ubuntu@dev:~/myprojects$ scrapy startproject demo
New Scrapy project 'demo' created in:
    /home/ubuntu/myprojects/demo

You can start your first spider with:
    cd demo
    scrapy genspider example example.com


# 目录结构如下
.
├── demo
│   ├── __init__.py
│   ├── items.py
│   ├── pipelines.py
│   ├── settings.py
│   └── spiders
│       └── __init__.py
└── scrapy.cfg

1 或者直接从test中复制myapp,如果要改项目名字,记得修改scarpy.cfg中的名字

2 删除掉其中的demo/items.py demo/piplines.py,并使用myapp/settings.py,myapp/spipders/init.py 替掉原来的文件

3 在spiders目录下,创建page_xpath.py, item_xpath.py, item_field.py, spiders.py,编写以下内容

# spiders.py

# -*- coding:utf-8 -*

SPIDERS = { # 配置spider, spider名称一个字典,字典中为这个spider的一些自定义属性,可为空
    "bluefly": {}
}

# page_xpath.py

# -*- coding:utf-8 -*

PAGE_XPATH = { # 配置网站分类页中获取下一页链接的方式,具体策略参见wiki
    "bluefly": [
        '//*[@id="page-content"]//a[@rel="next"]/@href',
    ]
}

# item_xpath.py

# -*- coding:utf-8 -*

ITEM_XPATH = { # 配置网站分类页中获取商品页链接的方式,xpath表达式
    "bluefly": [
        '//ul[@class="mz-productlist-list mz-l-tiles"]/li//a[@class="mz-productlisting-title"]/@href',
    ]
}

# item_field

# -*- coding:utf-8 -*

ITEM_FIELD = { # 商品页中,所需信息的获取方式,具体策略参见wiki
    "bluefly": [
        ('product_id', {
            "xpath": [
                '//li[@itemprop="productID"]/text()',
            ],
        }),
        ('brand', {
            "xpath": [
                '//p[@class="mz-productbrand"]/a/text()',
            ],
        }),
        ('names', {
            "xpath": [
                '//span[@class="mz-breadcrumb-current"]/text()',
            ],
        }),
    ]
}

4 修改demo/settings.py 文件,或者直接新建localsettings.py,增加自定义配置,要修改的项目在settings.py已注明

5 启动redis

#如果没有安装redis,可以使用自带的custom-redis,配置文件中需写明CUSTOM_REDIS=True
custom-redis-server -p 6379

6 启动爬虫

cd demo
scrapy crawl bluefly

7 投放任务

# 使用自带的costom-redis 需要加上 --custom
# 投放分类链接
feed -c test_01 -s bluefly -u "http://www.bluefly.com/assortment/the-boot-shop-overarching/women/shoes" --custom
# 投放项目链接,支持多个项目链接一起投放,把每个链接按行放到一个文件中即可
feed -c test_04 -s ashford -uf item.txt --custom

8 查看任务状态

# 使用自带的costom-redis 需要加上 --custom
check test_01 --custom

DECUMENT

参见wiki

本人python工作经验一年多,接触爬虫不到半年,开发这个框架仅仅用于学习和交流,可能存在诸多缺陷和bug,欢迎大家为我指正,可以pull requests给我哦,谢谢!

webwalker's People

Contributors

shichaoma avatar cnaafhvk avatar

Watchers

嘿哟传说 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.