python 爬虫 实现增量去重和定时爬取实例
2020-03-11 17:22
816 查看
前言: 在爬虫过程中,我们可能需要重复的爬取同一个网站,为了避免重复的数据存入我们的数据库中 通过实现增量去重 去解决这一问题 本文还针对了那些需要实时更新的网站 增加了一个定时爬取的功能;
本文作者同开源中国(殊途同归_);
解决思路:
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
以上这篇python 爬虫 实现增量去重和定时爬取实例就是小编分享给大家的全部内容了,希望能给大家一个参考
您可能感兴趣的文章:
- python爬虫开发之使用python爬虫库requests,urllib与今日头条搜索功能爬取搜索内容实例
- python爬虫开发之selenium模块详细使用方法与实例全解
- python爬虫开发之PyQuery模块详细使用方法与实例全解
- python爬虫开发之urllib模块详细使用方法与实例全解
- python爬虫开发之Request模块从安装到详细使用方法与实例全解
- Python爬虫程序架构和运行流程原理解析
- python爬虫开发之Beautiful Soup模块从安装到详细使用方法与实例
- Python网络爬虫信息提取mooc代码实例
- Python爬虫实现模拟点击动态页面
- Python反爬虫伪装浏览器进行爬虫
- python爬虫库scrapy简单使用实例详解
- 详解python 破解网站反爬虫的两种简单方法
- python爬虫模块URL管理器模块用法解析
- 使用Python爬虫库BeautifulSoup遍历文档树并对标签进行操作详解
- Python爬虫库BeautifulSoup获取对象(标签)名,属性,内容,注释
- Python爬虫库BeautifulSoup的介绍与简单使用实例
- 使用Python爬虫库requests发送表单数据和JSON数据
- python爬虫开发之使用Python爬虫库requests多线程抓取猫眼电影TOP100实例
相关文章推荐
- python 爬虫 实现增量去重和定时爬取实例
- Python Scrapy爬虫入门 - 使用Crontab实现Scrapy爬虫的定时执行
- Python爬虫实现验证码登录代码实例
- 浅析python实现scrapy定时执行爬虫
- Python爬虫实现爬取百度百科词条功能实例
- Python爬虫利用cookie实现模拟登陆实例详解
- Python爬虫实例——基于BeautifulSoup和requests实现
- Python爬虫实例——基于urlib、urlib和re实现
- python利用urllib实现爬取京东网站商品图片的爬虫实例
- Python爬虫实现爬取京东手机页面的图片(实例代码)
- python 每天如何定时启动爬虫任务(实现方法分享)
- Python实现爬虫从网络上下载文档的实例代码
- Python scrapy增量爬取实例及实现过程解析
- Python scrapy增量爬取实例及实现过程解析
- Python实现定时爬虫
- python实现scrapy定时执行爬虫
- Python 爬虫实例(10)—— 四行代码实现刷 博客园 阅读数量
- python实现单例模式,从始至终都只有一个实例
- 知道创宇爬虫程序的实现-Python
- 二种python发送邮件实例讲解(python发邮件附件可以使用email模块实现)