pyspider框架学习

1.pyspider的安装
pip install pyspider
2. 启动pyspider: 输入 pyspider all
在这里插入图片描述
但是启动pyspider 经常遇到没法正常启动,阻塞到 result_worker starting… ,感觉应该是源码有bug 存在,不过没关系,重新在开一个窗口,执行一下 pyspider all 就可以了。
3. 在通过浏览器访问:localhost:5000/
在这里插入图片描述
进入源码编写界面:
在这里插入图片描述查看result结果:
在这里插入图片描述
查看数据库存储:在这里插入图片描述
源码可直接运行,先自行在mysql中手动创建表和字段,原本想在代码里链接数据库的时候通过代码创建表和字段,好像有点问题,暂时先注释掉,后续在来看这个问题:

from pyspider.libs.base_handler import *
import pymysql
 
 
class Handler(BaseHandler):
    crawl_config = {
    }
    # 连接数据库
    def __init__(self):
        self.db = pymysql.connect(host='127.0.0.1',port=3306,db='qunar',user='root',passwd='123456',
                                  charset='utf8',use_unicode=True)
        #self.cursor = self.db.cursor()
        #sql_qunae = 'create table if not exists qunar1(url varchar(200) not null,\
        #title varchar(200) not null,\
        #date varchar(100) not null,\
        #day  varchar(100) not null,\
        #who  varchar(100) not null,\
        #text varchar(1000) not null,\
        #image varchar(200) not null)'
        #self.cursor.execute(sql_qunae)
 
    def save_in_mysql(self, url, title, date, day, who, text, image):
        try:
            cursor = self.db.cursor()
            sql = 'INSERT INTO qunar1(url, title, date, day, who, text, image) \
            VALUES (%s, %s , %s, %s, %s, %s, %s)'   # 插入数据库的SQL语句
            print(sql)
            cursor.execute(sql, (url, title, date, day, who, text, image))
            print(cursor.lastrowid)
            self.db.commit()
            self.db.close()
        except Exception as e:
            print(e)
            self.db.rollback()
     @every(minutes=24 * 60)
    def on_start(self):
        self.crawl('http://travel.qunar.com/travelbook/list.htm', callback=self.index_page)
 
    @config(age=10 * 24 * 60 * 60)
    def index_page(self, response):
        for each in response.doc('li > .tit > a').items():
            self.crawl(each.attr.href, callback=self.detail_page, fetch_type='js')
        next_url = response.doc('.next').attr.href
        self.crawl(next_url, callback=self.index_page)
 
    @config(priority=2)
    def detail_page(self, response):
        url = response.url
        title = response.doc('title').text()
        date = response.doc('.when .data').text()
        day = response.doc('.howlong .data').text()
        who = response.doc('.who .data').text()
        text = response.doc('#b_panel_schedule').text()[0:100].replace('\"', '\'', 10)
        image = response.doc('.cover_img').attr.src
        self.save_in_mysql(url, title, date, day, who, text, image)  
        return {
            "url": response.url,
            "title": response.doc('title').text(),
            "date": response.doc('.when .data').text(),
            "day": response.doc('.howlong .data').text(),
            "who": response.doc('.who .data').text(),
            "text": response.doc('#b_panel_schedule').text(),
            "image": response.doc('.cover_img').attr.src
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值