python 爬虫 实现增量去重和定时爬取实例
作者:X_Special 时间:2022-01-26 01:01:37
前言: 在爬虫过程中,我们可能需要重复的爬取同一个网站,为了避免重复的数据存入我们的数据库中 通过实现增量去重 去解决这一问题 本文还针对了那些需要实时更新的网站 增加了一个定时爬取的功能;
本文作者同开源中国(殊途同归_);
解决思路:
1.获取目标url
2.解析网页
3.存入数据库(增量去重)
4.异常处理
5.实时更新(定时爬取)
下面为数据库的配置 mysql_congif.py:
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
来源:https://blog.csdn.net/X_Special/article/details/80989345
标签:python,爬虫,去重,爬取
0
投稿
猜你喜欢
python基础之定义类和对象详解
2023-06-15 05:35:12
改善IE6中a与a:hover的背景支持
2009-11-27 18:50:00
解决matplotlib库show()方法不显示图片的问题
2021-08-25 19:53:34
Python基于回溯法子集树模板解决全排列问题示例
2023-12-18 21:25:04
web.py在SAE中的Session问题解决方法(使用mysql存储)
2024-01-28 02:17:15
PHP之CI框架学习讲解
2023-07-03 21:25:13
Flask和pyecharts实现动态数据可视化
2022-06-15 04:21:20
js编写的语法高亮引擎工具
2008-05-25 13:27:00
Python+OpenCV图片局部区域像素值处理改进版详解
2022-06-24 11:38:56
详解在spring中使用JdbcTemplate操作数据库的几种方式
2024-01-29 09:29:50
windows10在visual studio2019下配置使用openCV4.3.0
2021-10-23 12:23:31
pycharm中leetcode插件使用图文详解
2022-09-19 19:19:43
基于Vue实现图书管理功能
2024-04-27 16:16:59
OpenCV+python手势识别框架和实例讲解
2021-09-06 19:47:43
golang实现整型和字节数组之间的转换操作
2024-02-11 00:13:08
js刷新页面方法大全
2023-08-06 21:05:34
python实现AI聊天机器人详解流程
2022-12-11 23:57:37
python使用numpy实现直方图反向投影示例
2022-09-18 11:28:55
JavaScript对象的property属性详解
2024-05-05 09:22:57
conda虚拟环境默认路径的修改方法
2022-07-02 03:14:50