python增量爬虫
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增量爬虫的更多相关文章
- python增量爬虫pyspider
1.为了能够将爬取到的数据存入本地数据库,现在本地创建一个MySQL数据库example,然后 在数据库中建立一张表格test,示例如下: DROP TABLE IF EXISTS `test`; C ...
- 第三百六十二节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)基本的索引和文档CRUD操作、增、删、改、查
第三百六十二节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)基本的索引和文档CRUD操作.增.删.改.查 elasticsearch(搜索引擎)基本的索引 ...
- 四十一 Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)基本的索引和文档CRUD操作、增、删、改、查
elasticsearch(搜索引擎)基本的索引和文档CRUD操作 也就是基本的索引和文档.增.删.改.查.操作 注意:以下操作都是在kibana里操作的 elasticsearch(搜索引擎)都是基 ...
- 《实战Python网络爬虫》- 感想
端午节假期过了,之前一直在做出行准备,后面旅游完又休息了一下,最近才恢复状态. 端午假期最后一天收到一个快递,回去打开,发现是微信抽奖中的一本书,黄永祥的<实战Python网络爬虫>. 去 ...
- 零基础如何快速学习好Python网络爬虫?
Python网络爬虫上手很快,能够尽早入门,可是想精通确实是需求些时间,需求达到爬虫工程师的级别更是需求煞费苦心了,接下来共享的学习道路是针对小白或许学习Python网络爬虫不久的同伴们. 学习网络爬 ...
- Python简单爬虫入门三
我们继续研究BeautifulSoup分类打印输出 Python简单爬虫入门一 Python简单爬虫入门二 前两部主要讲述我们如何用BeautifulSoup怎去抓取网页信息以及获取相应的图片标题等信 ...
- Ubuntu下配置python完成爬虫任务(笔记一)
Ubuntu下配置python完成爬虫任务(笔记一) 目标: 作为一个.NET汪,是时候去学习一下Linux下的操作了.为此选择了python来边学习Linux,边学python,熟能生巧嘛. 前期目 ...
- Python简单爬虫入门二
接着上一次爬虫我们继续研究BeautifulSoup Python简单爬虫入门一 上一次我们爬虫我们已经成功的爬下了网页的源代码,那么这一次我们将继续来写怎么抓去具体想要的元素 首先回顾以下我们Bea ...
- [Python] 网络爬虫和正则表达式学习总结
以前在学校做科研都是直接利用网上共享的一些数据,就像我们经常说的dataset.beachmark等等.但是,对于实际的工业需求来说,爬取网络的数据是必须的并且是首要的.最近在国内一家互联网公司实习, ...
随机推荐
- leetcode218 天际线问题
来自leetcode题解:扫描线法AlgsCG class Solution { public: vector<vector<int>> getSkyline(vector&l ...
- Android MineType
概述 多用途互联网邮件扩展(MIME,Multipurpose Internet Mail Extensions)是一个互联网标准,它扩展了电子邮件标准,使其能够支持非ASCII字符.二进制格式附件等 ...
- java ldap用户密码md5加密
在这里不过多介绍ldap,因为这样的文章特别多,这里就简单直接的记录这一个问题. 在springboot中通过引入spring-boot-starter-data-ldap,使用LdapTemplat ...
- iOS tableHeaderView有默认高度?
在给tableView设置tableHeaderView的时候发现,如果设置tableView.tableHeaderView = [UIView new] , 这里未设置tableView的高度,但 ...
- openstack核心组件--glance镜像(2)
一.glance介绍: Glance是Openstack项目中负责镜像管理的模块,其功能包括虚拟机镜像的查找.注册和检索等. Glance提供Restful API可以查询虚 ...
- java+web文件的上传和下载代码
一般10M以下的文件上传通过设置Web.Config,再用VS自带的FileUpload控件就可以了,但是如果要上传100M甚至1G的文件就不能这样上传了.我这里分享一下我自己开发的一套大文件上传控件 ...
- JAVA 基础编程练习题7 【程序 7 处理字符串】
7 [程序 7 处理字符串] 题目:输入一行字符,分别统计出其中英文字母.空格.数字和其它字符的个数. 程序分析:利用 while 语句,条件为输入的字符不为'\n'. package cskaoya ...
- 关于Content-Type中application/x-www-form-urlencoded 和 multipart/form-data的区别及用法
http://blog.csdn.net/soonfly/article/details/52082547 Form的enctype属性表示页面表单数据向服务器传输时的编码方式, 常用有两种:appl ...
- charles简明教程
charles是一款强大的抓包软件,他可以帮助开发者制造后端数据也可以帮助测试者抓包分析. 一.电脑上安装charles软件包 charles当然有各个版本,一般选择较新的版本,而不会选择最新的版本. ...
- SQL Server 中 ROWLOCK 行级锁
一.ROWLOCK的使用 1.ROWLOCK行级锁确保,在用户取得被更新的行,到该行进行更新,这段时间内不被其它用户所修改.因而行级锁即可保证数据的一致性,又能提高数据操作的并发性. 2.ROWLOC ...