寫在開始:
很多程序設計入門的新手,語法學習了很久、代碼也敲了不少、各種模塊也有接觸,但要開發一個具體的項目,還是無從下手。
本文旨在拋磚引玉,以實例的開發過程,讓讀者完整的瞭解程序開發各個環節。
實例項目的效果及功能簡介
1、自動拉取百度知道,個人中心的待回答問題列表。並且在發現有新問題的時候,進行音效提醒。
2、對已拉取的數據進行篩選過濾。
3、生成提問人信息的URL地址,可以在瀏覽器中查看。
4、生成問題詳情的的URL地址,可以在瀏覽器中查看。
5、調用百度翻譯接口,翻譯輸入的文本。
6、隱藏系統控制檯,把控制檯信息,顯示到自己的GUI界面(實例用Text控件顯示)。
7、多線程運行業務邏輯,後臺自動拉取問題列表,與翻譯或其他操作可以同時執行。
第1篇:實例介紹及數據準備
第2篇:暫無
第3篇:暫無
第4篇:暫無
…
標題程序的主要數據來源是百度知道的網站信息,獲取過程就是常說的爬蟲。
由於百度知道的網頁是動態的,所以不能直接通過請求URL獲取網頁內容。
對知道個人中心頁面的請求過程進行分析。
1、分析後發現,網頁是通過一個push的接口,請求到數據對網頁進行渲染。
接口請求的表單
2、通過unreadcount接口獲取各種消息數量。
3、還有一些其他接口,本實例沒有用到,這裏就不介紹了。
4、另外上述的數據是需要用戶登錄的,實例使用網頁已登錄的cookie進行請求。
可以自行在瀏覽器的控制檯查看,也可以使用下面的程序讀取:
# 讀取百度知道登錄cookie
import sqlite3
import urllib3
import os
import json
import sys
import base64
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
def dpapi_decrypt(encrypted):
import ctypes
import ctypes.wintypes
class DATA_BLOB(ctypes.Structure):
_fields_ = [('cbData', ctypes.wintypes.DWORD),
('pbData', ctypes.POINTER(ctypes.c_char))]
p = ctypes.create_string_buffer(encrypted, len(encrypted))
blobin = DATA_BLOB(ctypes.sizeof(p), p)
blobout = DATA_BLOB()
retval = ctypes.windll.crypt32.CryptUnprotectData(
ctypes.byref(blobin), None, None, None, None, 0, ctypes.byref(blobout))
if not retval:
raise ctypes.WinError()
result = ctypes.string_at(blobout.pbData, blobout.cbData)
ctypes.windll.kernel32.LocalFree(blobout.pbData)
return result
def aes_decrypt(encrypted_txt):
with open(os.path.join(os.environ['LOCALAPPDATA'],
r"Google\Chrome\User Data\Local State"), encoding='utf-8', mode="r") as f:
jsn = json.loads(str(f.readline()))
encoded_key = jsn["os_crypt"]["encrypted_key"]
encrypted_key = base64.b64decode(encoded_key.encode())
encrypted_key = encrypted_key[5:]
key = dpapi_decrypt(encrypted_key)
nonce = encrypted_txt[3:15]
cipher = Cipher(algorithms.AES(key), None, backend=default_backend())
cipher.mode = modes.GCM(nonce)
decryptor = cipher.decryptor()
return decryptor.update(encrypted_txt[15:])
def chrome_decrypt(encrypted_txt):
if sys.platform == 'win32':
try:
if encrypted_txt[:4] == b'x01x00x00x00':
decrypted_txt = dpapi_decrypt(encrypted_txt)
return decrypted_txt.decode()
elif encrypted_txt[:3] == b'v10':
decrypted_txt = aes_decrypt(encrypted_txt)
return decrypted_txt[:-16].decode()
except WindowsError:
return None
else:
raise WindowsError
def get_cookies_from_chrome(domain):
sql = f'SELECT name, encrypted_value as value FROM cookies where host_key like "%{domain}%"'
filename = os.path.join(os.environ['USERPROFILE'], r'AppData\Local\Google\Chrome\User Data\default\Cookies')
con = sqlite3.connect(filename)
con.row_factory = sqlite3.Row
cur = con.cursor()
cur.execute(sql)
cookie = {}
for row in cur:
if row['value'] is not None:
name = row['name']
value = chrome_decrypt(row['value'])
if value is not None:
cookie[name] = value
return cookie
if __name__ == '__main__':
domain = 'baidu.com' # 目標網站域名
cookie = get_cookies_from_chrome(domain)
for x in cookie:
print("%s: %s"%(x,cookie[x]))
5、百度翻譯的數據請求比較麻煩。需要獲取一個叫singn的校驗數據,纔可以請求到翻譯結果。這裏暫時不講,源碼有對關鍵步驟進行註釋。
本文結束,下一篇開始tkinter GUI界面的搭建。也是實例開發過程最主要的部分。