一、不写入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

}

数据库建设:

结果:

python3爬取数据存入mysql_pyspider 数据存入Mysql--Python3相关推荐

  1. Python3爬取汽车目标经销商数据

    Python3爬取汽车目标经销商数据 本文采用Python3进行语法编写,Python3与Python2中的函数会有所不同,但是相差不大,具体的问题可以百度找到,因有朋友在做汽车方面的业务,因此需要一 ...

  2. Python3爬取豆瓣图书Top250并存入csv文件中

    本文抓取豆瓣图书Top250中的相关资讯,可以练习对于网页结构的分析及提取. 下面先导入相关的包,并伪装成浏览器访问: import requests from lxml import etree i ...

  3. python3爬取中国药学科学数据

    今天我表弟说帮忙爬一下中国药学科学数据,导出json格式给他.一共18万条数据. 看了一下网站http://pharm.ncmi.cn/dataContent/admin/index.jsp?subm ...

  4. Python3 爬取豆瓣电影信息

    原文链接: Python3 爬取豆瓣电影信息 上一篇: python3 爬取电影信息 下一篇: neo4j 查询 豆瓣api https://developers.douban.com/wiki/?t ...

  5. Python3爬取企查查网站的企业年表并存入MySQL

    Python3爬取企查查网站的企业年表并存入MySQL 本篇博客的主要内容:爬取企查查网站的企业年报数据,存到mysql中,为了方便记录,分成两个模块来写: 第一个模块是爬取数据+解析数据,并将数据存 ...

  6. python3爬取巨潮资讯网站年报数据

    python3爬取巨潮资讯网站年报数据 2018年年底巨潮资讯http://www.cninfo.com.cn改版了,之前实习生从网上找的脚本不能用了,因此重新修改了下爬取脚本.最初脚本的原链接忘了, ...

  7. python3爬取数据_python3爬取巨潮资讯网站年报数据

    python3爬取巨潮资讯网站年报数据 2018年年底巨潮资讯http://www.cninfo.com.cn改版了,之前实习生从网上找的脚本不能用了,因此重新修改了下爬取脚本.最初脚本的原链接忘了, ...

  8. python3爬取巨潮资讯网的年报数据

    python3爬取巨潮资讯网的年报数据 前期准备: 需要用到的库: 完整代码: 前期准备: 巨潮资讯网有反爬虫机制,所以先打开巨潮资讯网的年报板块,看看有什么解决办法. 巨潮咨询年报板块 可以通过这样 ...

  9. Python3爬取OpenStreetMap平台的城市道路交通网数据

    Python3 爬取OpenStreetMap平台的城市道路交通网数据 前言 思路 代码 效果 结语 前言 论文需要城市道路信息数据,OpenStreetMap平台是一个开源免费的全球地图信息平台,但 ...

  10. Python3爬取今日头条文章视频数据,完美解决as、cp、_signature的加密方法(2020-6-29版)

    前言 在这里我就不再一一介绍每个步骤的具体操作了,因为在爬取老版今日头条数据的时候都已经讲的非常清楚了,所以在这里我只会在重点上讲述这个是这么实现的,如果想要看具体步骤请先去看我今日头条的文章内容,里 ...

最新文章

  1. (原創) 如何讓Linux開機時,不要在圖形介面顯示開機訊息? (OS) (Linux) (CentOS)
  2. (Matrix3D)坑爹的flash帮助文档
  3. mac mysql 移动硬盘_MAC一些高能过程记录(一些没必要的坑)
  4. python语言的特点有没有面向过程_Python 入门基础之面向对象过程-面向过程概述...
  5. vm15不支持linux,每个处理程序的多个VMExtensions不支持操作系统类型'Linux
  6. Spring MVC起步(一)
  7. HashMap学习之transient
  8. 修改linux最大文件句柄数
  9. mysql怎么让id重新开始自增
  10. TOPSIS法 —— matlab
  11. python中几种去重的方法
  12. python writeline_Python文件写入函数 write()和writelines()
  13. keyberos认证问题导致GSS initiate failed
  14. canvas之-------水滴扩散特效
  15. Allegro_Outline,RouteKeepin倒圆角
  16. JavaWeb Ajax二级联动Bootstrap的基本使用
  17. java中间件学习1-java中间件的定义
  18. GRASPIT安装流程
  19. antd table 超出显示省略号无效果
  20. 质数乘积 -LintCode

热门文章

  1. 西湖区政府门户网站项目签约西部动力We7网站群系统
  2. javascript学习笔记(十五) 间歇调用和超时调用
  3. [置顶] 我行贿了 I Paid A Bribe! --印度IPAB中国官方网站 - 民间反腐网站“我行贿了”网址大盘点...
  4. SQL数据库备份还原命令
  5. 2008已经到来,我们怎能原地踏步!
  6. [XJOI]noip44 T3还有这种操作
  7. json对象与字符串互转
  8. npm和gulp学习
  9. 【AC】九度OJ题目1153:括号匹配问题
  10. 解决thrift: ···No such file or directory问题