国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

python 爬蟲 實(shí)現(xiàn)增量去重和定時(shí)爬取實(shí)例

瀏覽:77日期:2022-08-05 14:50:32

前言: 在爬蟲過程中,我們可能需要重復(fù)的爬取同一個(gè)網(wǎng)站,為了避免重復(fù)的數(shù)據(jù)存入我們的數(shù)據(jù)庫中 通過實(shí)現(xiàn)增量去重 去解決這一問題 本文還針對(duì)了那些需要實(shí)時(shí)更新的網(wǎng)站 增加了一個(gè)定時(shí)爬取的功能;

本文作者同開源中國(殊途同歸_);

解決思路:

1.獲取目標(biāo)url

2.解析網(wǎng)頁

3.存入數(shù)據(jù)庫(增量去重)

4.異常處理

5.實(shí)時(shí)更新(定時(shí)爬取)

下面為數(shù)據(jù)庫的配置 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進(jìn)行網(wǎng)頁解析# 實(shí)現(xiàn)了增量去重# 實(shí)現(xiàn)了定時(shí)爬取import datetimeimport time from bs4 import BeautifulSoupimport requests from mysql_config import insert_dbfrom 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):%st時(shí)間:%st號(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í)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 长汀县| 白河县| 南投县| 绥中县| 修水县| 弥勒县| 渭南市| 聂拉木县| 会宁县| 宝清县| 孝昌县| 平舆县| 邹平县| 苍梧县| 菏泽市| 宜城市| 保康县| 健康| 方城县| 平陆县| 孝感市| 长葛市| 衡水市| 阿拉善左旗| 高唐县| 阜康市| 乌恰县| 文水县| 襄汾县| 油尖旺区| 和顺县| 岚皋县| 长沙市| 巴楚县| 南京市| 丘北县| 博兴县| 县级市| 赣榆县| 绵竹市| 昌吉市|