main.py (主程序)
1 from bs4 import BeautifulSoup
2 import urllib.request
3 import urllib.parse
4 import sqlite3
5 import setting
6 import func
7 import time
8 import re
9 import os
10
11
12 # ===============================================================
13 def main():
14 # 建表
15 func.connect('athlete.db', setting.sql_createTable)
16
17 # 参量
18 judge = 0
19 error_list_old = []
20
21 # 大循环
22 while True: # 设置此值避免无限循环。推荐值:50
23 if judge >= 50:
24 print('本次循环结束!请开启下次循环')
25 break
26 error_list_new = []
27 # 第一次爬取,还没有产生 error文件
28 if 'error_url.txt' not in os.listdir():
29 url_list_outer = func.get_url(setting.file_name) # 获取 url列表
30 func.data_handle(url_list_outer, error_list_new)
31 if len(error_list_new) == 0:
32 print('已完成对 %s 内容的爬取!' % setting.file_name)
33 break
34 # 写入文件
35 func.creat_error_txt(error_list_new)
36 judge += 1
37
38 # 第二次爬取
39 else:
40 # 读数据
41 error_list_old = func.creat_error_list('error_url.txt')
42 # 爬取
43 func.data_handle(error_list_old, error_list_new)
44 if len(error_list_new) == 0:
45 os.remove('error_url.txt') # 当没有新的 error_url时需要删除.txt文件
46 print('已完成对 %s 内容的爬取!' % setting.file_name)
47 break
48 # 写入文件
49 func.creat_error_txt(error_list_new)
50 judge += 1