scrapy存储mysql
scrapy 数据存储mysql

#spider.py
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
from Cwpjt.items import CwpjtItem class FulongSpider(CrawlSpider):
name = 'fulong'
allowed_domains = ['sina.com.cn']
start_urls = ['http://sina.com.cn/']
'http://news.sina.com.cn/c/2017-05-09/doc-ifyeycte9324112.shtml'
rules = (
Rule(LinkExtractor(allow=('.*?/[0-9]{4}.[0-9]{2}.[0-9]{2}.doc-.*?shtml'),allow_domains=('sina.com.cn')),
callback='parse_item', follow=True),
) def parse_item(self, response):
i = CwpjtItem()
i['name']=response.xpath('/html/head/title/text()').extract()
i['kws'] = response.xpath('/html/head/meta[@name="keywords"]/@content').extract()
#i['domain_id'] = response.xpath('//input[@id="sid"]/@value').extract()
#i['name'] = response.xpath('//div[@id="name"]').extract()
#i['description'] = response.xpath('//div[@id="description"]').extract()
return i

pipeline

import pymysql
from pymysql import connections
class CwpjtPipeline(object):
def __init__(self):
self.conn = pymysql.connect(host='127.0.0.1',user='root',passwd='123456',db ='mydb')
self.cursor = self.conn.cursor()
def process_item(self, item, spider):
name = item['name'][0]
kws = item['kws'][0]
sql ="insert into hehe(title,kws) VALUES(%s,%s)"
self.cursor.execute(sql,(name,kws,))
self.conn.commit()
return item
def close_spider(self,spider):
self.conn.close()

item

import scrapy class CwpjtItem(scrapy.Item):
# define the fields for your item here like:
name = scrapy.Field()
kws = scrapy.Field()

scrapy存储mysql的更多相关文章
- 分布式数据存储 - MySQL双主复制
上篇文章<分布式数据存储 - MySQL主从复制>,我们说到MySQL主从复制很好的保障了从库,读的高可用性.so,问题来了: 1.针对主库,写的高可用性又是如何做到高可用性? 2.如果需 ...
- Scrapy小技巧-MySQL存储, MYSQL拼接
这两天上班接手,别人留下来的爬虫发现一个很好玩的 SQL脚本拼接. 只要你的Scrapy Field字段名字和 数据库字段的名字 一样.那么恭喜你你就可以拷贝这段SQL拼接脚本.进行MySQL入库处理 ...
- scrapy 异步存储mysql
1.在setting中设置MySQL连接信息 HOST='101.201.70.139'MYSQL_DBNAME='anttest'MYSQL_PASSWORD='Myjr678!@#'MYSQL_U ...
- scrapy 数据存储mysql
#spider.pyfrom scrapy.linkextractors import LinkExtractor from scrapy.spiders import CrawlSpider, Ru ...
- 解析数据存储MySQL
为了适应不同项目对不同感兴趣属性的解析存储,数据存储结构采用纵向的属性列表方式,即一个url页面多个属性存储多条记录方式,并且按照text,html, data,num几大典型类型分别对应存储. 创建 ...
- 分布式数据存储 - MySQL主从复制高可用方案
前面几篇文章说道MySQL数据库的高可用方案主从复制.主从复制的延迟产生原因.延迟检测及延迟解决方案(并未从根本上解决),这种主从复制方案保证数据的冗余的同时可以做读写分离来分担系统压力但是并非是高可 ...
- 解决Emoji存储MySQL报错问题
在解决之前,得先说明一下为什么会出现报错,Emoji表情占用4个字节,但是MySQL数据库UTF-8编码最多只能存储3个字节,就会导致存储不进去 如何解决Emoji存储问题 mysql 的 utf8编 ...
- scrapy连接MySQL
Scrapy中连接MySQL所需要做的工作如下: 1.settings中需要设置的部分 # 启动管道组件 ITEM_PIPELINES = { 'QianChengWuYu.mongoDBPiplel ...
- 数据存储之关系型数据库存储---MySQL存储
MySQL的存储 利用PyMySQL连接MySQL 连接数据库 import pymysql # 连接MySQL MySQL在本地运行 用户名为root 密码为123456 默认端口3306 db = ...
随机推荐
- JS框架_(JQuery.js)纯css3进度条动画
百度云盘 传送门 密码:wirc 进度条动画效果: <!DOCTYPE html> <html lang="zh"> <head> <me ...
- Spring data redis-StringRedisTemplate 用法
Spring-data-redis为spring-data模块中对redis的支持部分,简称为“SDR”,提供了基于jedis客户端API的高度封装以及与spring容器的整合,事实上jedis客户端 ...
- BootStrap之X-editable插件使用
项目背景 刚加入公司的新项目,主要在做开发工作.由于是新手,本周的工作是配合另外一个同事写前台页面.前台框架是Bootstrap,本文主要介绍一下项目需求的一个功能——表格行内编辑事件. 使用X-ed ...
- spring 手动注册bean
//将applicationContext转换为ConfigurableApplicationContext ConfigurableApplicationContext configurableAp ...
- 【学习】SpringBoot之自定义拦截器
/** * 自定义拦截器 **/ @Configuration//声明这是一个拦截器 public class MyInterceptor extends WebMvcConfigurerAdapte ...
- 使用Desktop App Converter打包桌面应用程序
打包具有安装程序 (.msi) 的应用程序 DesktopAppConverter.exe -Installer C:\Installer\MyAppSetup.msi -Destination C: ...
- Other | 十招教你找到海量PPT模板
转载自:https://www.douban.com/note/330962457/ 问:PPT模板是什么含义? 答: 先假定你们要的是这种网上到处泛滥成灾的主题PPT吧,下面请耐心看到最后,秋叶老师 ...
- AndroidStudio 插件 之 Findbugs 安装与简单使用教程
http://blog.csdn.net/u013132758/article/details/70187846 http://blog.csdn.net/jdsjlzx/article/detail ...
- apache配置静态缓存
配置静态缓存:节省带宽,加快访问速度,提高用户体验.<IfModule mod_expires.c> ExpiresActive on ExpiresByType image/gif &q ...
- 四十二:数据库之SQLAlchemy之数据查询懒加载技术
懒加载在一对多,或者多对多的时候,如果要获取多的这一部分的数据的时候,通过一个relationship定义好对应关系就可以全部获取,此时获取到的数据是list,但是有时候不想获取全部数据,如果要进行数 ...