51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 1732|回复: 1
打印 上一主题 下一主题

Pyspider 爬虫使用说明

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2019-3-7 15:02:35 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 老白的释然 于 2019-3-7 15:12 编辑

一、    pyspider安装

pip install pyspider

启动之后如下:

Windows系统

Linux系统


二、pyspider例子

html bodydiv#container.ss-list div.main div.box div.content div.siteList ul li h3 a
将路径加上>即可,变成形如

html>body>div#container.ss-list>div.main>div.box>div.content>div.siteList>ul>li>h3>a


注意:有时候可能需要修改某些内容,如去掉(tbody)

例子:http://school.163.aoshu.com/school/249907/,获取学校网址。

FireFox浏览器抓取结果

html>body>div.wrapper.bgf>div.school>article.schoolintro>dl.clearfix>dd>table>tbody>tr:last-child>td:last-child



实际使用时候为:

table>tr:nth-child(6)>td:nth-child(2)



备注:

tbody可能是页面自动生成的,可以通过打开源代码查看,确定是否需要去掉!

源码没有<tbody>这个元素,而浏览器打开是有<tobdy >这个元素的。

源码如下:

浏览器查看结果,含有<tbody>这个元素。


代码如下:

爬取豆瓣分类数据

from pyspider.libs.base_handler import *



class Handler(BaseHandler):

    crawl_config = {

    }



    @every(minutes=24 * 60)

    def on_start(self):

       self.crawl('http://movie.douban.com/tag/', callback=self.index_page)



    @config(age=10 * 24 * 60 *60)

    def index_page(self,response):

        for each inresponse.doc('#content>div>div.article>table:nth-child(9)>tbody>tr>td>a').items():

           a=each.attr.href.replace('www','movie')

           self.crawl(a,callback=self.list_page)



    def list_page(self,response):

    #获得电影详细内容链接并交给下一个函数处理

       for each inresponse.doc('td > .pl2 > a').items():

           self.crawl(each.attr.href,callback=self.detail_page)

           #翻页,然后继续由list_page函数处理  

       for each inresponse.doc('.next > a').items():

           self.crawl(each.attr.href,callback=self.list_page)



    def detail_page(self,response):

       return {

               "url": response.url,

               "title":response.doc('* > * > div#wrapper >div#content > h1 > span').text(),

               "rate":response.doc('.rating_num').text(),

               "导演":response.doc('#info> span:nth-child(1) > span.attrs > a').text()

           }  


例2、爬取dir001网址的分类信息

#!/usr/bin/env python

# -*- encoding: utf-8 -*-

# Created on 2016-09-08 09:22:15

# Project: dir003



from pyspider.libs.base_handler import *





class Handler(BaseHandler):

    crawl_config = {

    }



    @every(minutes=24 * 60)

    def on_start(self):

       self.crawl('http://www.dir001.com/category', callback=self.index_page)



    @config(age=100 * 24 * 60 *60)

    def index_page(self,response):

        for each inresponse.doc('html>body>div#container.ss-category>div.main>div.box.catalogbox>div.content>dl>dd>ul>li>a').items():

           self.crawl(each.attr.href,callback=self.list_page)



    @config(priority=2)

    def list_page(self,response):

        for each inresponse.doc('html>body>div#container.ss-list>div.main>div.box>div.content>div.siteList>ul>li>h3>a').items():

           self.crawl(each.attr.href,callback=self.detail_page)

        for each inresponse.doc('html>body>div#container.ss-list>div.main>div.box>div.content>div.pagelink>form#pageForm>ul.yiiPager>li:last-child>a').items():

           self.crawl(each.attr.href,callback=self.list_page)   



    def detail_page(self,response):

        return {

               "url":response.doc('html>body>div#container.site-content>div.box.contenInfo>div.content>div.contentMain>div.siteInfo>div.movieDetail>p>span.site-domain').text(),#url地址

              "mulu":response.doc('html>body>div#container.site-content>div.box.contenInfo>div.content>div.contentMain>div.siteInfo>div.movieDetail>p:nth-child(3)').text(),#目录

              "location":response.doc('html>body>div#container.site-content>div.box.contenInfo>div.content>div.contentMain>div.siteInfo>div.movieDetail>p:nth-child(4)').text(),#地址

              "Rank":response.doc('html>body>div#container.site-content>div.box.contenInfo>div.content>div.contentMain>div.siteInfo>div.movieDetail>p>b.alexarank').text(),#等级

              "on_clicks":response.doc('html>body>div#container.site-content>div.box.contenInfo>div.content>div.contentMain>div.siteInfo>div.movieDetail>p>span.c_red').text(),       #点击次数



               "description":response.doc("html>body>div#container.site-content>div.box.contenInfo>div.content>div.contentMain>div.siteInfo>div.movieDetail>p>span.site-description").text()

           }


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

本版积分规则

关闭

站长推荐上一条 /1 下一条

小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

GMT+8, 2024-4-25 23:06 , Processed in 0.066932 second(s), 22 queries .

Powered by Discuz! X3.2

© 2001-2024 Comsenz Inc.

快速回复 返回顶部 返回列表