Python全系列 教程
3567个小节阅读:5929.8k
目录
鸿蒙应用开发
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
目标网站:中国福利彩票网 双色球往期数据
阳光开奖 (cwl.gov.cn) http://www.cwl.gov.cn/ygkj/wqkjgg/
xxxxxxxxxx
class MongoPipeline:
def open_spider(self, spider):
self.client = pymongo.MongoClient()
self.ssq = self.client.bjsxt.ssq
def process_item(self, item, spider):
if item.get('code') =='2022086':
self.ssq.insert_one(item)
return item
def close_spider(self, spider):
self.client.close()
# pip install pymysql==1.0.2
import pymysql
from scrapy.exceptions import DropItem
class MySQLPipeline:
def open_spider(self, spider):
# 创建数据库连接
self.client = pymysql.connect(host='192.168.31.151',port=3306,user='root',password='123',db='bjsxt',charset='utf8')
# 获取游标
self.cursor = self.client.cursor()
def process_item(self, item, spider):
if item.get('code') =='2022086':
raise DropItem('2022086 数据已经在mongo保存过了')
# 写入数据库SQL
sql = 'insert into t_ssq (id,code,red,blue) values (0,%s,%s,%s)'
# 写的数据参数
args = (item['code'],item['red'],item['blue'])
# 执行SQL
self.cursor.execute(sql,args)
# 提交事务
self.client.commit()
return item
def close_spider(self, spider):
self.cursor.close()
self.client.close()