我正在尝试使用https://softwarerecs.stackexchange.com/questions/30389/industrial-handheld-qrcode-scanner-open-url-in-browser中提到的脚本从键盘输入或QR码扫描程序中获取URL,并在浏览器中自动打开它。
当我在python 3.6中运行这个脚本时,它打印[]而不是实际输入。当我删除re.findall时,它正确格式化,但我不确定re.findall中的哪个部分导致了问题。
import pyHook
import pythoncom
import re
import webbrowser
endDomains = ".com|.net|.org|.edu|.gov|.mil|.aero|.asia|.biz|.cat|.coop|.info|.int|.jobs|.mobi|.museum|.name|.post|.pro|.tel|.travel".split("|")
chars = ""
def pressed_chars(event):
global chars
if event.Ascii:
char = chr(event.Ascii)
if event.Ascii == 3:
quit()
else:
chars += char
try:
urls = re.findall(r'http[s]?://([a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', chars)
print(urls)
except:
urls = re.findall(r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', chars)
print(urls)
if len(urls) > 0:
for url in urls:
for i in endDomains:
if i in url:
webbrowser.open_new_tab(url)
chars = ""
break
return 0
proc = pyHook.HookManager()
proc.KeyDown = pressed_chars
proc.HookKeyboard()
pythoncom.PumpMessages()
答案 0 :(得分:0)
我发现这个问题是一个缺少的间隔,所以这个
http[s]?://([a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+
应该是
http[s]?://|([a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+