本文主要介紹Qthread的使用方法,用來解決python後臺爬數據時窗口未響應問題。使用這樣的結構可以實現後臺爬數據的同時實時UI顯示,克服假死問題
我的程序是給一個url來提取數據格式化顯示的,界面如下:
一、數據獲取及處理代碼
1.數據獲取
@logger.catch()
def get_html(url):
logger.debug('----開始執行get_html----')
try:
logger.debug('執行try')
response = requests.get(url)
if response.status_code == 200:
html = response.text
# html = html.encode('ISO-8859-1')
# html = html.decode('utf-8')
logger.debug('----執行get_html完畢----')
return html
else:
logger.debug('網絡連接故障')
return None
except RequestException:
print(RequestException)
return None