scrapy实战8关于数据异步写入mysql:
环境: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:的更多相关文章
- Scrapy爬取豆瓣图书数据并写入MySQL
项目地址 BookSpider 介绍 本篇涉及的内容主要是获取分类下的所有图书数据,并写入MySQL 准备 Python3.6.Scrapy.Twisted.MySQLdb等 演示 代码 一.创建项目 ...
- Python+Scrapy+Crawlspider 爬取数据且存入MySQL数据库
1.Scrapy使用流程 1-1.使用Terminal终端创建工程,输入指令:scrapy startproject ProName 1-2.进入工程目录:cd ProName 1-3.创建爬虫文件( ...
- MySQL实战 | 03 - 谁动了我的数据:浅析MySQL的事务隔离级别
原文链接:这一次,带你搞清楚MySQL的事务隔离级别! 使用过关系型数据库的,应该都事务的概念有所了解,知道事务有 ACID 四个基本属性:原子性(Atomicity).一致性(Consistency ...
- HTTP协议与使用Python获取数据并写入MySQL
一.Http协议 二.Https协议 三.使用Python获取数据 (1)urlib (2)GET请求 (3)POST请求 四.爬取豆瓣电影实战 1.思路 (1)在浏览器中输入https://movi ...
- scrapy 爬取知乎问题、答案 ,并异步写入数据库(mysql)
python版本 python2.7 爬取知乎流程: 一 .分析 在访问知乎首页的时候(https://www.zhihu.com),在没有登录的情况下,会进行重定向到(https://www. ...
- python scrapy 实战简书网站保存数据到mysql
1:创建项目 2:创建爬虫 3:编写start.py文件用于运行爬虫程序 # -*- coding:utf-8 -*- #作者: baikai #创建时间: 2018/12/14 14:09 #文件: ...
- Python使用Scrapy框架爬取数据存入CSV文件(Python爬虫实战4)
1. Scrapy框架 Scrapy是python下实现爬虫功能的框架,能够将数据解析.数据处理.数据存储合为一体功能的爬虫框架. 2. Scrapy安装 1. 安装依赖包 yum install g ...
- Scrapy实战篇(五)之爬取历史天气数据
本篇文章我们以抓取历史天气数据为例,简单说明数据抓取的两种方式: 1.一般简单或者较小量的数据需求,我们以requests(selenum)+beautiful的方式抓取数据 2.当我们需要的数据量较 ...
- Flink 1.9 实战:使用 SQL 读取 Kafka 并写入 MySQL
上周六在深圳分享了<Flink SQL 1.9.0 技术内幕和最佳实践>,会后许多小伙伴对最后演示环节的 Demo 代码非常感兴趣,迫不及待地想尝试下,所以写了这篇文章分享下这份代码.希望 ...
随机推荐
- SGI STL中内存池的实现
最近这两天研究了一下SGI STL中的内存池, 网上对于这一块的讲解很多, 但是要么讲的不完整, 要么讲的不够简单(至少对于我这样的初学者来讲是这样的...), 所以接下来我将把我对于对于SGI ST ...
- ABP缓存示例
private readonly ICacheManager _cacheManager; public ProgrammeManage(ICacheManager cacheManager) { _ ...
- WPF中自定义的DataTemplate中的控件,在Window_Loaded事件中加载机制初探
原文:WPF中自定义的DataTemplate中的控件,在Window_Loaded事件中加载机制初探 最近因为项目需要,开始学习如何使用WPF开发桌面程序.使用WPF一段时间之后,感 ...
- Delphi 调用C/C++的Dll(stdcall关键字, 会导致函数名分裂. 此时函数名变成_stdadd@8)
delphi调用C++写的Dll, 当然这个Dll要求是非MFC的Dll, 这样子才能被delphi调用. 根据C++定义函数的情况, Delphi有不同的相对应的处理方法.1. 声明中不加__std ...
- Wpf发送接收 win32消息
#region WPF发送和接收win32消息 public const int WM_GETTEXT = 0x0D; public const int WM_SETTEXT = 0x0C; publ ...
- .Net Core中使用NodeJs加解密DES,MD5,AES,REA
鉴于使用.net core我们的加解密也同时迁移到了跨平台上,我使用的是NodeJs加解密的.废话不多说了,还是来干活吧. 1.创建Node项目 2.添加package.json { "n ...
- qt mingw64版本编译报错:incorrect register `%rax' used with `l' suffix(movl要改成mov)
环境:WIN10 编译器:mingw64的g++.exe 我的目的是把程序编译成64bit版本的,所以一开始遇到该错误是丈二和尚摸不着头脑,google了一圈也没找到准确的答案.后来从某些回答中大概发 ...
- Devart Blog
How to combine data from several sources using SQL and VirtualQueryhttp://blog.devart.com/how-to-com ...
- C++&Win32写的空当接龙
上学期做课程设计,老师让我做windows自带的空当接龙游戏,写了一个礼拜,完全仿windows的呵呵.不过也不全一样,有一些细节一直没有时间弄,没办法最近比较懒... 与windows下的相比,我做 ...
- Model-View-Controller Explained in C++
The Permanent URL is: Model-View-Controller Explained in C++. The Model-View-Controller (MVC) is not ...