一、不写入Mysql

以爬取哪儿网为例。

以下为脚本:

from pyspider.libs.base_handler import *

class Handler(BaseHandler):

crawl_config = {

}

@every(minutes=24 * 60)

def on_start(self):

self.crawl('https://travel.qunar.com/travelbook/list.htm', callback=self.index_page, validate_cert=False)

@config(age=100 * 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, validate_cert=False, fetch_type='js')

next = response.doc('.next').attr.href

self.crawl(next, callback=self.index_page)

@config(priority=2)

def detail_page(self, response):

return {

"url": response.url,

"title": response.doc('#booktitle').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').text(),

}

这个脚本里只是单纯的将结果打印在pyspider 的web ui中,并没有存到其它地方。

二、存入Mysql中

插入数据库的话,需要我们在调用它之前定义一个save_in_mysql函数。 并且需要将连接数据库等初始化放在__init__函数中。

注: pymysql.connect('localhost', '账号', '密码', '数据库', charset='utf8')

# 连接数据库

def __init__(self):

self.db = pymysql.connect('localhost', 'root', 'root', 'qunar', charset='utf8')

def save_in_mysql(self, url, title, date, day, who, text, image):

try:

cursor = self.db.cursor()

sql = 'INSERT INTO qunar(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()

except Exception as e:

print(e)

self.db.rollback()

然后在detail_page中调用save_in_mysql函数:

@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

}

三、完整代码、数据库建设及运行结果 (代码可直接跑)

#!/usr/bin/env python

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

# Created on 2019-07-02 21:37:08

# Project: qunar

from pyspider.libs.base_handler import *

import pymysql

class Handler(BaseHandler):

crawl_config = {

}

# 连接数据库

def __init__(self):

self.db = pymysql.connect('localhost', 'root', 'root', 'qunar', charset='utf8')

def save_in_mysql(self, url, title, date, day, who, text, image):

try:

cursor = self.db.cursor()

sql = 'INSERT INTO qunar(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()

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

}

数据库建设:

结果:

原文链接:https://www.cnblogs.com/liangmingshen/p/11124811.html

如有疑问请与原作者联系

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com

特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

pyspider配置mysql,pyspider 数据存入Mysql--Python3相关推荐

  1. python读取串口数据保存到mysql数据库_Python3读取Excel数据存入MySQL的方法

    Python是数据分析的强大利器. 利用Python做数据分析,第一步就是学习如何读取日常工作中产生各种excel报表并存入数据中,方便后续数据处理. 这里向大家分享python3如何使用xlrd读取 ...

  2. scrapy爬虫数据存入mysql数据库

    上篇博客使用scrapy框架爬取豆瓣电影top250信息将各种信息通过json存在文件中,不过对数据的进一步使用显然放在数据库中更加方便,这里将数据存入mysql数据库以便以后利用. 运行环境: 1. ...

  3. flume 数据存入mysql(二)对敏感信息加密

    前面写了flume 将数据存入mysql,但是有个问题,就是配置文件中暴露了我的mysql 的地址和帐号密码.这个是很危险的事情.所以这里就对mysql 的配置信息进行简单加密处理. 加密我才用 ba ...

  4. 如何将数据存入mysql_怎样将数据存入mysql数据库

    怎样将数据存入mysql数据库 mip版  关注:231  答案:2  悬赏:80 解决时间 2021-01-18 14:57 已解决 2021-01-17 20:37 怎样将数据存入mysql数据库 ...

  5. php mysql 读取数据_PHP MySQL 读取数据

    全屏 PHP MySQL 读取数据 从 MySQL 数据库读取数据 SELECT 语句用于从数据表中读取数据:SELECT column_name(s) FROM table_name 我们可以使用 ...

  6. Python爬取股票数据存入mysql数据库,获取股票(最新、最高、今开、成交量、成交额、量比、换手率、涨幅等)支持多线程+数据库连接池

    项目简介 (Python)爬虫 + MySQL + Redis项目. 爬取下来的数据可用于后续的数据分析(我计划将其用于我的毕业设计). 未来会将数据分析的可视化部署到服务器上, 并添加股票降价通知的 ...

  7. python将数据存入数据库_python3 两种方法将数据存入mysql数据库

    原博文 2017-09-22 18:25 − 方法一:(数据量小的时候推荐使用这种) 第一步:pip install mysqlclient 这里我没有报错 也许你可能会报错Read timed ou ...

  8. scrapy mysql数据库_Python3学习系列(十三):Scrapy将数据存入Mysql数据库

    前言: 下面给大家介绍将下载的数据存入到Mysql数据库的用法,数据来源是Mooc的课程. 代码实现: items.py from scrapy import Item,Field class Moo ...

  9. scrapy框架爬取Boss直聘,数据存入mysql

    自从上次用了scrapy爬取豆瓣电影后,发现scrapy除了入门相对request较难外,各方面都挺好的,速度很快,还有各个功能模块,以及django类似的各种中间件组成一个完善的系统框架,需要一点一 ...

  10. mysql dba系统学习(12)mysql的数据文件 mysql dba系统学习(13)mysql的体系结构

    mysql的数据文件 一,系统参数datadir 在MySQL 中每一个数据库都会在定义好(或者默认)的数据目录下存在一个以数据库名字命名的文件夹,用来存放该数据库中各种表数据文件 datadir指定 ...

最新文章

  1. Eclipse-Java代码规范和质量检查插件-阿里编码规约
  2. python3 subprocess.Popen 报错 No such file or directory
  3. OpenRASP xss算法的几种绕过方法
  4. 中国靶材行业需求前景分析及发展形势研究报告2021版
  5. SAP UI5 未来发展的趋势之一:拥抱 TypeScript
  6. Note cancel request的实现原理
  7. 战地5服务器不显示ping怎么回事,战地5常见bug解决办法分享
  8. ipc原理linux,传统的Linux中IPC通信原理
  9. 【java奇思妙想】关于JavaScript实现全选,全不选以及反选功能的示例
  10. 空间中任一点到超平面的距离公式的推导过程
  11. Visual Studio报错:由于代码已经过优化或者本机框架位于调用堆栈之上,无法计算表达式的值...
  12. [大数据 ]Apache大数据项目目录
  13. 扒一扒那些叫欧拉的定理们(二)——简单多面体欧拉定理的证明
  14. 51单片机温控风扇仿真原理图 C语言程序,51单片机温控风扇(含程序+原理图+仿真+PCB)...
  15. c语言如何画函数图形,c语言绘制函数曲线
  16. oracle rac 各日志,oracle rac 日志体系结构!
  17. [C++] 中的trivial destructor
  18. 文本批量替换的正则表达式
  19. 微信支付出现故障,程序员的高薪理所当然
  20. Yocto系列讲解[理论篇] 3 - meta layer recipe class概念介绍

热门文章

  1. 一本关于ChatGPT的书《ChatGPT 革命:了解大型语言模型的力量》免费下载
  2. error Unnecessary escape character: \/ no-useless-escape
  3. 公司注册资金需要实缴吗?实缴和认缴有什么区别?
  4. [创业-17]:财务报表之综述
  5. 涨握在线|微软宣布400亿回购计划,华为发布mate30对战Iphone 11
  6. mysql 服务被杀毒软甲删掉之后,恢复方法
  7. win7 计算机属性 灰,win7系统文件夹属性中隐藏选项显示灰色不可改的解决方法...
  8. XMind8 破解安装 【含免安装版本】亲测可用
  9. 2019顺丰科技笔试
  10. 计算机数据分析试题,计算机二级考试真题-Excel-小马-公务员考试成绩数据分析...