环境:python3

爬取网址:腾讯社招(http://hr.tencent.com/position.php?keywords=&tid=0&start=0#a)总共2202条数据

pipelines.py

 from twisted.enterprise import adbapi
import pymysql
import pymysql.cursors class MysqlTwistedPipeline(object):
def __init__(self,dbpool):
self.dbpool=dbpool @classmethod
def from_settings(cls,settings):
dbpool=adbapi.ConnectionPool("pymysql",host=settings["MYSQL_HOST"],db=settings["MYSQL_DBNAME"],user=settings["MYSQL_USER"],password=settings["MYSQL_PASSWORD"],charset="utf8", cursorclass=pymysql.cursors.DictCursor,
use_unicode=True)
return cls(dbpool) def process_item(self,item,spider):
# 使用twisted将mysql插入变成异步执行
self.dbpool.runInteraction(self.do_insert,item) def do_insert(self,cursor,item):
# 执行具体的插入
# 根据不同的item 构建不同的sql语句并插入到mysql中
insert_sql, params = item.get_insert_sql()
cursor.execute(insert_sql, params)

items.py

 import scrapy

 class TencentItem(scrapy.Item):

     positionname=scrapy.Field()
positionlink=scrapy.Field()
positionType=scrapy.Field()
positionNum=scrapy.Field()
positionLocation=scrapy.Field()
publishTime=scrapy.Field() def get_insert_sql(self):
insert_sql="""
insert into tencent(positionname,positionlink,positionType,positionNum,positionLocation,publishTime)
VALUES (%s,%s,%s,%s,%s,%s) """
params=(
self['positionname'], self['positionlink'], self['positionType'], self['positionNum'],
self['positionLocation'], self['publishTime']
)
return insert_sql,params

settings.py

BOT_NAME = 'tencent'

SPIDER_MODULES = ['tencent.spiders']
NEWSPIDER_MODULE = 'tencent.spiders' ROBOTSTXT_OBEY = False (不用分布式可忽略下面三项)
SCHEDULER = "scrapy_redis.scheduler.Scheduler"
DUPEFILTER_CLASS = "scrapy_redis.dupefilter.RFPDupeFilter"
SCHEDULER_PERSIST = True DOWNLOAD_DELAY = 2 DEFAULT_REQUEST_HEADERS = {
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:53.0) Gecko/20100101 Firefox/53.0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
#'Accept-Language': 'en',
} ITEM_PIPELINES = {
'scrapy_redis.pipelines.RedisPipeline':400,(不用分布式可忽略)
'tencent.pipelines.MysqlTwistedPipeline': 300,
}
REDIS_HOST = '172.21.118.56'(分布式主机ip 不用分布式可忽略)
REDIS_PORT = 6379(不用分布式可忽略) MYSQL_HOST = "127.0.0.1"
MYSQL_DBNAME = "tencent"(自己数据库名字)
MYSQL_USER = "usrername"(用户名)
MYSQL_PASSWORD = "userpassword"(密码)

spiders/Tencent.py

from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import Rule
from scrapy_redis.spiders import RedisCrawlSpider
from tencent.items import TencentItem class TencentSpider(RedisCrawlSpider):
name = "Tencent"
allowed_domains = ["tencent.com"]
redis_key = 'TencentSpider:start_urls' page_link=LinkExtractor(allow=(r"start=\d+")) rules=[
Rule(page_link,callback = "parseContent",follow=True)
] def parseContent(self, response):
list=response.xpath('//tr[@class="even"] | //tr[@class="odd"]')
for infos in list:
item=TencentItem()
item['positionname']=infos.xpath("./td[1]/a/text()").extract()[0]
item['positionlink']=infos.xpath("./td[1]/a/@href").extract()[0]
item['positionType']=infos.xpath("./td[2]/text()").extract()
item['positionNum']=infos.xpath("./td[3]/text()").extract()[0]
item['positionLocation']=infos.xpath("./td[4]/text()").extract()[0]
item['publishTime']=infos.xpath("./td[5]/text()").extract()[0] yield item

scrapy实战8关于数据异步写入mysql:的更多相关文章

  1. Scrapy爬取豆瓣图书数据并写入MySQL

    项目地址 BookSpider 介绍 本篇涉及的内容主要是获取分类下的所有图书数据,并写入MySQL 准备 Python3.6.Scrapy.Twisted.MySQLdb等 演示 代码 一.创建项目 ...

  2. Python+Scrapy+Crawlspider 爬取数据且存入MySQL数据库

    1.Scrapy使用流程 1-1.使用Terminal终端创建工程,输入指令:scrapy startproject ProName 1-2.进入工程目录:cd ProName 1-3.创建爬虫文件( ...

  3. MySQL实战 | 03 - 谁动了我的数据:浅析MySQL的事务隔离级别

    原文链接:这一次,带你搞清楚MySQL的事务隔离级别! 使用过关系型数据库的,应该都事务的概念有所了解,知道事务有 ACID 四个基本属性:原子性(Atomicity).一致性(Consistency ...

  4. HTTP协议与使用Python获取数据并写入MySQL

    一.Http协议 二.Https协议 三.使用Python获取数据 (1)urlib (2)GET请求 (3)POST请求 四.爬取豆瓣电影实战 1.思路 (1)在浏览器中输入https://movi ...

  5. scrapy 爬取知乎问题、答案 ,并异步写入数据库(mysql)

      python版本  python2.7 爬取知乎流程: 一 .分析 在访问知乎首页的时候(https://www.zhihu.com),在没有登录的情况下,会进行重定向到(https://www. ...

  6. python scrapy 实战简书网站保存数据到mysql

    1:创建项目 2:创建爬虫 3:编写start.py文件用于运行爬虫程序 # -*- coding:utf-8 -*- #作者: baikai #创建时间: 2018/12/14 14:09 #文件: ...

  7. Python使用Scrapy框架爬取数据存入CSV文件(Python爬虫实战4)

    1. Scrapy框架 Scrapy是python下实现爬虫功能的框架,能够将数据解析.数据处理.数据存储合为一体功能的爬虫框架. 2. Scrapy安装 1. 安装依赖包 yum install g ...

  8. Scrapy实战篇(五)之爬取历史天气数据

    本篇文章我们以抓取历史天气数据为例,简单说明数据抓取的两种方式: 1.一般简单或者较小量的数据需求,我们以requests(selenum)+beautiful的方式抓取数据 2.当我们需要的数据量较 ...

  9. Flink 1.9 实战:使用 SQL 读取 Kafka 并写入 MySQL

    上周六在深圳分享了<Flink SQL 1.9.0 技术内幕和最佳实践>,会后许多小伙伴对最后演示环节的 Demo 代码非常感兴趣,迫不及待地想尝试下,所以写了这篇文章分享下这份代码.希望 ...

随机推荐

  1. 多线程Parallel和Task

    不管是Parallel还是Task,最里面都是线程池(里面是线程)当开启多个任务后,系统会根据当前的线程池的资源进行分配,任务则进行等待Parallel可以对系统的CPU进行设置,可以最大程度上榨干系 ...

  2. gnuradio companion 找不到第三方模块gr-osmosdr的问题

    我使用了来自Ettus的gnuradio软件包,之后安装了gr-osmosdr 以在gnuradio中调用RTL电视棒. 但是在gnuradio companion找不到来自rtlsdr-source ...

  3. MVC 自定义路由规则

    using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mv ...

  4. 为什么腾讯总能做出好产品?(在互联网行业,往往仅凭一个关键产品就足以改变整个公司的格局)MSN失败在不以用户体验为中心

    投递人 itwriter 发布于 2017-07-10 11:16 评论(36) 有3401人阅读 原文链接 [收藏] « » 本文来自微信公众号“郑志昊 Peter”,作者李翔.郑志昊:博客园经授权 ...

  5. miniui处理多重子表级联,一次性提交多表数据的ui要点

    在一个ui界面上 有a,b,c三个表 a表只有一条记录,b表有多条记录,c表有多条记录 b是a的子表,c是b的子表 都是一对多关系 一次性下载相关联的c表记录 然后mini-datagrid采用cli ...

  6. WPF用DirectSound播放声音

    示例代码: var fileName = @"D:\WindowsLogon.wav"; DevicesCollection sound_devices = new Devices ...

  7. Git基本用法(一)

    使用Git正常的工作流 创建/修改文件 使用git add <file1> <file2> <file3>...将文件添加至本地的缓冲区Index中 使用git c ...

  8. Natively Compiled Code: A Comeback?

    RAD Studio and Natively Compiled Code In today's development landscape, natively compiled code is ma ...

  9. delphi中move函数的正确理解(const和var一样,都是传地址,所以Move是传地址,而恰恰不是传值)太精彩了 good

    我们能看到以下代码var pSource,pDest:PChar;     len: integer;.......................//一些代码Move(pSource,pDest,l ...

  10. 只言片语 - cell 图片复用问题

    一. 今日做项目遇到图片复用问题,返回cell高度相同,由于网络不好出现图片复用,发现问题   Cell 图片加载方法如下: - (void)sd_setImageWithURL:(NSURL *)u ...