使用 python 在 Web 浏览器中打开 QR 码
Open QRCode in Webbrowser with python
我有一个工业条码扫描仪。
我想扫描一个包含 URI 的二维码,它应该会在机器的默认浏览器中直接自动打开该 URI。
我有以下代码:
# -*- coding: utf-8 -*-
import pyHook
import pythoncom
import re
import webbrowser
endDomains = ".de|.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
proc = pyHook.HookManager()
proc.KeyDown = pressed_chars
proc.HookKeyboard()
pythoncom.PumpMessages()
但是它打不开我的浏览器。
谁能帮帮我?
操作系统是 Windows 10,安装了 Python 2.7 + Pyhook。
Python output
下面的 python 代码做了我想要的:
import pyHook
import pythoncom
import httplib
import getpass
import shutil
import sys
import re
import webbrowser
endDomains = "end|thisistheend|.com".split("|")
chars = ""
def OnKeyBoardEvent(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
hook_manager = pyHook.HookManager()
hook_manager.KeyDown = OnKeyBoardEvent
hook_manager.HookKeyboard()
pythoncom.PumpMessages()
当用户输入以下语法时,它会在 Internet Explorer 中打开一个网站:
https://website.com
我有一个工业条码扫描仪。
我想扫描一个包含 URI 的二维码,它应该会在机器的默认浏览器中直接自动打开该 URI。
我有以下代码:
# -*- coding: utf-8 -*-
import pyHook
import pythoncom
import re
import webbrowser
endDomains = ".de|.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
proc = pyHook.HookManager()
proc.KeyDown = pressed_chars
proc.HookKeyboard()
pythoncom.PumpMessages()
但是它打不开我的浏览器。 谁能帮帮我?
操作系统是 Windows 10,安装了 Python 2.7 + Pyhook。
Python output
下面的 python 代码做了我想要的:
import pyHook
import pythoncom
import httplib
import getpass
import shutil
import sys
import re
import webbrowser
endDomains = "end|thisistheend|.com".split("|")
chars = ""
def OnKeyBoardEvent(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
hook_manager = pyHook.HookManager()
hook_manager.KeyDown = OnKeyBoardEvent
hook_manager.HookKeyboard()
pythoncom.PumpMessages()
当用户输入以下语法时,它会在 Internet Explorer 中打开一个网站: https://website.com