python慢在哪里_求大神分析一下我的python脚本慢在哪里?
請大神們分析下這個python腳本為什么會運行這么慢.
初衷: 公司有個料號系統(tǒng), 在網(wǎng)站上輸入料號就能查詢相關(guān)的資料, 一般情況下, 我會ctrl+c, 打開網(wǎng)站, ctrl+v, 點擊查詢, 得到資料.
腳本實現(xiàn)方式: 我ctrl+c料號, 腳本調(diào)用windows的clipboard API, 拿到所復(fù)制的text, 然后用requests模擬正常瀏覽器的GET, 最后通過webbrowser模塊打開得到的HTML.
問題: 速度很慢, 可以說比我手動在瀏覽器上看要慢. 基本超過6s..有什么辦法可以改良呢?
謝謝!
"""
work flow: get text content like "23.34239.394"( PN format) from
clipboard, making a GET request using requests, with URL parameter
PartNumber=text, and then open its html text with webbrowser module.
"""
import ctypes
import requests
import os
import webbrowser
def spec():
# clipboard format TEXT defined by MS
CF_TEXT = 1
kernel32 = ctypes.windll.kernel32
user32 = ctypes.windll.user32
user32.OpenClipboard(0)
if user32.IsClipboardFormatAvailable(CF_TEXT):
data = user32.GetClipboardData(CF_TEXT)
data_locked = kernel32.GlobalLock(data)
text = ctypes.c_char_p(data_locked)
print(text.value)
kernel32.GlobalUnlock(data_locked)
else:
print('no text in clipboard')
user32.CloseClipboard()
# decode bytes to unicode string
s = text.value.decode()
# fork a browser GET request
url = 'http://wpqssvr.wistron.com.tw:7001/wpqs/servlet/COM.qpart.Attachment'
para = {'PartNumber': s}
h = {
'Accept': 'text/html',
'Connection': 'keep-alive',
'Host': 'wpqssvr.wistron.com.tw:7001',
'Accept-Language': 'zh-TW',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Trident/7.0;rv:11.0) like Gecko'
}
# make the request and stores the html into temp file, open with IE
try:
r = requests.get(url, params = para, headers=h)
path = os.path.abspath('temp.html')
url = 'file://' + path
with open(path, 'w') as f:
f.write(r.text)
webbrowser.open_new_tab(url)
except:
print('open url or open file fails')
raise
exit()
if __name__ == '__main__':
spec()
與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的python慢在哪里_求大神分析一下我的python脚本慢在哪里?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: javascript字典中添加数组_如何
- 下一篇: eplan单线原理图多线原理图_【原创分