代码
1 import sqlite3 as db 2 import time 3 import json 4 import datetime 5 from selenium import webdriver 6 from selenium.webdriver.common.by import By 7 from selenium.webdriver.support import expected_conditions as EC 8 from selenium.webdriver.support.wait import WebDriverWait 9 from selenium.webdriver.chrome.options import Options 10 # 从SQLite文件中读取数据 11 def readFronSqllite(db_path,exectCmd): 12 conn = db.connect(db_path) # 该 API 打开一个到 SQLite 数据库文件 database 的链接,如果数据库成功打开,则返回一个连接对象 13 cursor=conn.cursor() # 该例程创建一个 cursor,将在 Python 数据库编程中用到。 14 conn.row_factory=db.Row # 可访问列信息 15 cursor.execute(exectCmd) #该例程执行一个 SQL 语句 16 rows=cursor.fetchall() #该例程获取查询结果集中所有(剩余)的行,返回一个列表。当没有可用的行时,则返回一个空的列表。 17 return rows 18 #print(rows[0][2]) # 选择某一列数据 19 20 21 def Trans_time(visite_time): 22 true_time = visite_time / 1000000 - 11644473600 23 time_normal = time.gmtime(true_time) # 转换为普通时间格式(时间数组) 24 dt = time.strftime("%Y-%m-%d", time_normal) # 格式化为需要的格式 25 # print(dt) 26 from_dt = datetime.datetime(2021, 10, 4) 27 start_date = datetime.datetime.strptime(dt, "%Y-%m-%d") 28 if from_dt>start_date: 29 print(start_date) 30 dt="!!" 31 return dt 32 33 def Get_condition_json(): 34 article_info = {} 35 Data = [] 36 rows = readFronSqllite('./data/History.db', "select url,title,last_visit_time from urls") 37 i = 0 38 for row in rows: 39 try: 40 if ('github' in row[0] and 'issues' in row[0]): 41 data_parent_child = json.loads(json.dumps(article_info)) 42 data_parent_child['url'] = row[0] 43 data_parent_child['title'] = row[1] 44 data_parent_child['time'] = Trans_time(row[2]) 45 if (data_parent_child['time'] =="!!" or "search" in row[0]): 46 continue 47 i = i + 1 48 data_parent_child['id'] = i 49 Data.append(data_parent_child) 50 except: 51 continue 52 return Data 53 54 def Save_json(url,data): 55 with open(url, "w") as f: 56 json.dump(data, f) 57 58 def Open_web(url): 59 js = "window.open('{}','_blank');" 60 if(Chrome_num==1): 61 driver.get(url) 62 else: 63 driver.execute_script(js.format(url)) 64 def Save_data(): 65 Data=Get_condition_json() 66 Save_json(json_url,Data) 67 print("加载入文件完成...") 68 print(Data) 69 def Read_data(url): 70 with open(json_url, 'r') as load_f: 71 Datas = json.load(load_f) 72 return Datas 73 if __name__=="__main__": 74 options = Options() 75 # options.headless = True#禁止打开 76 driver = webdriver.Chrome('D:\Program Apps\Google\Chrome\driver\chromedriver.exe',options=options) 77 Datas=[] 78 json_url="./data/data.json" 79 # Save_data() 80 81 Datas=Read_data(json_url) 82 Chrome_num=0 83 print(len(Datas)) 84 for data in Datas: 85 Chrome_num=Chrome_num+1 86 Num=data["id"] 87 if(Num>=0 and Num<=10): 88 print(data["id"]) 89 Open_web(data["url"])