python 爬蟲 實(shí)現(xiàn)增量去重和定時(shí)爬取實(shí)例
前言: 在爬蟲過(guò)程中,我們可能需要重復(fù)的爬取同一個(gè)網(wǎng)站,為了避免重復(fù)的數(shù)據(jù)存入我們的數(shù)據(jù)庫(kù)中 通過(guò)實(shí)現(xiàn)增量去重 去解決這一問(wèn)題 本文還針對(duì)了那些需要實(shí)時(shí)更新的網(wǎng)站 增加了一個(gè)定時(shí)爬取的功能;
本文作者同開源中國(guó)(殊途同歸_);
解決思路:
1.獲取目標(biāo)url
2.解析網(wǎng)頁(yè)
3.存入數(shù)據(jù)庫(kù)(增量去重)
4.異常處理
5.實(shí)時(shí)更新(定時(shí)爬?。?/p>
下面為數(shù)據(jù)庫(kù)的配置 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
接下來(lái)是主要代碼 test.py:
# 使用bs4進(jìn)行網(wǎng)頁(yè)解析 # 實(shí)現(xiàn)了增量去重 # 實(shí)現(xiàn)了定時(shí)爬取 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('期號(hào):%s\t時(shí)間:%s\t號(hào)碼:%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 已經(jīng)存在!' % 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 爬蟲 實(shí)現(xiàn)增量去重和定時(shí)爬取實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python實(shí)現(xiàn)發(fā)送郵件到自己郵箱
在日常開發(fā)中,我們經(jīng)常需要監(jiān)控應(yīng)用程序的狀態(tài),及時(shí)發(fā)現(xiàn)問(wèn)題并采取措施解決。而通過(guò)郵件發(fā)送報(bào)警信息則是一種常見的實(shí)現(xiàn)方式。本文就來(lái)介紹一下Python實(shí)現(xiàn)發(fā)送郵件到自己郵箱的方法2023-04-04python中l(wèi)eastsq函數(shù)的使用方法
這篇文章主要介紹了python中l(wèi)eastsq函數(shù)的使用方法,leastsq作用是最小化一組方程的平方和,下面文章舉例說(shuō)明詳細(xì)內(nèi)容,具有一的參考價(jià)值,需要的小伙伴可以參考一下2022-03-03PyQt5基本控件使用之消息彈出、用戶輸入、文件對(duì)話框的使用方法
本文主要介紹PyQt界面實(shí)現(xiàn)中常用的消息彈出對(duì)話框、提供用戶輸入的輸入框、打開文件獲取文件/目錄路徑的文件對(duì)話框。 本文主要針對(duì)這三種控件的主要場(chǎng)景進(jìn)行介紹。感興趣的朋友跟隨小編一起看看吧2019-08-08Python使用SQLAlchemy模塊實(shí)現(xiàn)操作數(shù)據(jù)庫(kù)
SQLAlchemy 是用Python編程語(yǔ)言開發(fā)的一個(gè)開源項(xiàng)目,它提供了SQL工具包和ORM對(duì)象關(guān)系映射工具,使用SQLAlchemy可以實(shí)現(xiàn)高效和高性能的數(shù)據(jù)庫(kù)訪問(wèn),下面我們就來(lái)學(xué)習(xí)一下SQLAlchemy模塊的具體應(yīng)用吧2023-11-11通過(guò)代碼實(shí)例了解Python sys模塊
這篇文章主要介紹了通過(guò)代碼實(shí)例了解Python sys模塊,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09詳解Tensorflow數(shù)據(jù)讀取有三種方式(next_batch)
本篇文章主要介紹了Tensorflow數(shù)據(jù)讀取有三種方式(next_batch),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-02-02python多線程實(shí)現(xiàn)同時(shí)執(zhí)行兩個(gè)while循環(huán)的操作
這篇文章主要介紹了python多線程實(shí)現(xiàn)同時(shí)執(zhí)行兩個(gè)while循環(huán)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-05-05