pyspider框架学习

匿名 (未验证) 提交于 2019-12-03 00:06:01

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         } 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!