import pymysql

def insert_db(db_table, issue, time_str, num_code):
host = '127.0.0.1'
user = 'root'
password = 'root'
port = 3306
db = 'lottery'
data_base = pymysql.connect(host=host, user=user, password=password, port=port, db=db)
cursor = data_base.cursor()
try:
sql = "INSERT INTO %s VALUES ('%s','%s','%s')" % (db_table, issue, time_str, num_code)
cursor.execute(sql)
data_base.commit()
except ValueError as e:
print(e)
data_base.rollback()
finally:
cursor.close()
data_base.close()
def select_db(issue, db_table):
host = '127.0.0.1'
user = 'root'
password = 'root'
port = 3306
db = 'lottery'
data_base = pymysql.connect(host=host, user=user, password=password, port=port, db=db)
cursor = data_base.cursor()
try:
sql = "SELECT '%s' FROM %s " % (issue, db_table)
cursor.execute(sql)
data_base.commit()
except ValueError as e:
print(e)
data_base.rollback()
finally:
return issue
# 接下来是主要代码
# test.py:
# 使用bs4进行网页解析
# 实现了增量去重
# 实现了定时爬取
import datetime
import time
from bs4 import BeautifulSoup
import requests
from mysql_config import insert_db
from mysql_config import select_db
def my_test():
db_table = 'lottery_table'
url = 'http://kj.13322.com/kl10_dkl10_history_dtoday.html'
res = requests.get(url)
content = res.content
soup = BeautifulSoup(content, 'html.parser', from_encoding='utf8')
c_t = soup.select('#trend_table')[0]
trs = c_t.contents[4:]
for tr in trs:
if tr == '\n':
continue
tds = tr.select('td')
issue = tds[1].text
time_str = tds[0].text
num_code = tr.table.text.replace('\n0', ',').replace('\n', ',').strip(',')
print('期号:%s\t时间:%s\t号码:%s' % (str(issue), str(time_str), str(num_code)))
issue_db = select_db(issue, db_table)
try:
if issue_db == issue:
insert_db(db_table, issue_db, time_str, num_code)
print('添加%s到%s成功' % (issue_db, db_table))
except Exception as e:
print('%s 已经存在!' % issue_db)
print(e) if __name__ == '__main__':
flag = 0
now = datetime.datetime.now()
sched_time = datetime.datetime(now.year, now.month, now.day, now.hour, now.minute, now.second) + \
datetime.timedelta(seconds=3)
while True:
now = datetime.datetime.now()
if sched_time < now:
time.sleep(3)
print(now)
my_test()
flag = 1
else:
if flag == 1:
sched_time = sched_time + datetime.timedelta(minutes=2)
flag = 0

python增量爬虫的更多相关文章

  1. python增量爬虫pyspider

    1.为了能够将爬取到的数据存入本地数据库,现在本地创建一个MySQL数据库example,然后 在数据库中建立一张表格test,示例如下: DROP TABLE IF EXISTS `test`; C ...

  2. 第三百六十二节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)基本的索引和文档CRUD操作、增、删、改、查

    第三百六十二节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)基本的索引和文档CRUD操作.增.删.改.查 elasticsearch(搜索引擎)基本的索引 ...

  3. 四十一 Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)基本的索引和文档CRUD操作、增、删、改、查

    elasticsearch(搜索引擎)基本的索引和文档CRUD操作 也就是基本的索引和文档.增.删.改.查.操作 注意:以下操作都是在kibana里操作的 elasticsearch(搜索引擎)都是基 ...

  4. 《实战Python网络爬虫》- 感想

    端午节假期过了,之前一直在做出行准备,后面旅游完又休息了一下,最近才恢复状态. 端午假期最后一天收到一个快递,回去打开,发现是微信抽奖中的一本书,黄永祥的<实战Python网络爬虫>. 去 ...

  5. 零基础如何快速学习好Python网络爬虫?

    Python网络爬虫上手很快,能够尽早入门,可是想精通确实是需求些时间,需求达到爬虫工程师的级别更是需求煞费苦心了,接下来共享的学习道路是针对小白或许学习Python网络爬虫不久的同伴们. 学习网络爬 ...

  6. Python简单爬虫入门三

    我们继续研究BeautifulSoup分类打印输出 Python简单爬虫入门一 Python简单爬虫入门二 前两部主要讲述我们如何用BeautifulSoup怎去抓取网页信息以及获取相应的图片标题等信 ...

  7. Ubuntu下配置python完成爬虫任务(笔记一)

    Ubuntu下配置python完成爬虫任务(笔记一) 目标: 作为一个.NET汪,是时候去学习一下Linux下的操作了.为此选择了python来边学习Linux,边学python,熟能生巧嘛. 前期目 ...

  8. Python简单爬虫入门二

    接着上一次爬虫我们继续研究BeautifulSoup Python简单爬虫入门一 上一次我们爬虫我们已经成功的爬下了网页的源代码,那么这一次我们将继续来写怎么抓去具体想要的元素 首先回顾以下我们Bea ...

  9. [Python] 网络爬虫和正则表达式学习总结

    以前在学校做科研都是直接利用网上共享的一些数据,就像我们经常说的dataset.beachmark等等.但是,对于实际的工业需求来说,爬取网络的数据是必须的并且是首要的.最近在国内一家互联网公司实习, ...

随机推荐

  1. 微服务中的CAP定律

    说到微服务,先给大家提一下CAP分布式应用知识吧,无论你微服务使用的是阿里云开源的Dubbo还是基于Springboot的一整套实现微服务的Springcloud都必须遵循CAP定理不然你所实现的分布 ...

  2. 让 Nginx 支持 WAF 防护功能实战

    ngx_lua_waf 安装说明文档 作者github地址: https://github.com/loveshell/ngx_lua_waf ———————————————————————————— ...

  3. 一百零四:CMS系统之修改邮箱界面

    在base.css中加一个全局的css控制宽度 .form-container{ width: 300px;} 视图 class ResetEmailView(views.MethodView): d ...

  4. powershell自动添加静态IP

    声明:其中脚本有参考其他作者,由于当时参考仓促,未能把作者一一列出,有机会会再找出原作者文件链接并附上,请见谅 参考: https://ss64.com/nt/netsh.html https://w ...

  5. asyncio模块实现单线程-多任务的异步协程

    本篇介绍基于asyncio模块,实现单线程-多任务的异步协程 基本概念 协程函数 协程函数: 定义形式为 async def 的函数; aysnc 在Python3.5+版本新增了aysnc和awai ...

  6. centos7安装kvm及kvm管理

    一.安装kvm 查看CPU是否支持虚拟化  grep -E 'svm|vmx' /proc/cpuinfo - vmx is for Intel processors - svm is for AMD ...

  7. 【JVM学习笔记】Class.forName方法学习

    三个参数的版本的源代码如下 doc文档翻译 使用给定的类加载器(即第3个参数)返回与具有给定字符串名称(第1个参数)的类或接口关联的Class对象.给定类或接口的完全限定名称(以getName返回的相 ...

  8. Python multiprocess模块(中)

    主要内容: 一. 锁 二. 信号量 三. 事件 通过event来完成红绿灯模型 四. 队列(重点) 队列实现进程间的通信 五. 生产者消费者模型 1. 初始版本(程序会阻塞住) 2. 升级版本一(通过 ...

  9. ubuntu14.04 安装 zoom.us

    https://support.zoom.us/hc/en-us/articles/204206269-Getting-Started-on-Linux http://askubuntu.com/qu ...

  10. vim中ctrl+z,jobs,fg命令用法

    例如:vi test.cctrl + z 将vi 打开的test.c停止并转到后台又打开一个vi buf.cctrl + z 将vi 打开的buf.c停止并转到后台此时通过jobs可以看到任务列表通过 ...