环境: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. Android开发四大件

    四大组件 Activity Activity是Android应用程序的界面,比如查看联系人.打电话.玩游戏的界面等一个应用程序通常包含多个Activity,即多个界面Activity通过布局管理各种V ...

  2. js 点谁谁哭

    <!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml"> ...

  3. ORM 集合

    1.EF   https://github.com/aspnet 2.Chloe.ORM http://www.cnblogs.com/so9527/p/5809089.html http://www ...

  4. Gralde 网络代理

    Gralde 网络代理 Gradle在编译项目的时候,需要下载一些依赖.墙外的网络就需要设置代理了. 设置的方法,见文档: Accessing the web through a HTTP proxy ...

  5. 微信小程序把玩(三十七)location API

    原文:微信小程序把玩(三十七)location API location API也就分这里分两种wx.getLocation(object)获取当前位置和wx.openLocation(object) ...

  6. C++没有库则寸步难行,有库则几乎可以做任何事情——Bjarne Stroustrupi

    "Without a good library, most interesting tasks are hard to do in C++; but given a good library ...

  7. OpenGL与Directx的区别

    OpenGL 只是图形函数库. DirectX 包含图形, 声音, 输入, 网络等模块. 单就图形而论, DirectX 的图形库性能不如 OpenGL OpenGL稳定,可跨平台使用.但 OpenG ...

  8. Gps坐标距离计算C#实现

    园子里找到两钟实现方式,做一记录,回头再认真学习,先拿来就用吧: 1.@旋风描述的算法: 场景:已知两个GPS点的经纬度坐标信息.计算两点的距离. 1. 距离/纬度关系 GPS: 22.514519, ...

  9. python中的变量,字符串,用户交互,if语句

    一:python介绍 python的创始人为吉多·范罗苏姆,创始时间是1989年. 1python是一门什么样的语言 python是一门解释型弱类型语言★ 弱类型:弱类型的变量可以变,强类型的变量不能 ...

  10. Flask学习之旅--数据库

    一.写在前面 在Web开发中,数据库操作是很重要的一部分,因为网站的很多重要信息都保存在数据库之中.而Flask在默认情况下是没有数据库.表单验证等功能的,但是可以用Flask-extension为W ...