


Python implements randomly calling a browser to open a web page
下面为大家分享一篇python实现随机调用一个浏览器打开网页,具有很好的参考价值,希望对大家有所帮助。一起过来看看吧
前两天总结了一下python爬虫 使用真实浏览器打开网页的两种方法总结
但那仅仅是总结一下而已,今天本文来实战演练一下
依然使用的是 webbrowser 这个模块 来调用浏览器
关于的三种打开方式在上一篇文章中已经说过了,这里不再赘述
如果没有特意注册,那么将会是使用默认的浏览器来打开网页,如下:
#默认浏览器 #coding:utf-8 import webbrowser as web #对导入的库进行重命名 def run_to_use_default_browser_open_url(url): web.open_new_tab(url) print 'run_to_use_default_browser_open_url open url ending ....'
真正的注册一个非默认浏览器:
这里先用的firfox浏览器
#firefox浏览器 def use_firefox_open_url(url): browser_path=r'C:\Program Files (x86)\Mozilla Firefox\firefox.exe' #这里的‘firefox'只是一个浏览器的代号,可以命名为自己认识的名字,只要浏览器路径正确 web.register('firefox', web.Mozilla('mozilla'), web.BackgroundBrowser(browser_path)) #web.get('firefox').open(url,new=1,autoraise=True) web.get('firefox').open_new_tab(url) print 'use_firefox_open_url open url ending ....'
解释一下这个注册函数当前的用法
web.register() 它的三个参数
第一个为 自己给浏览器重新命的名字, 主要目的是为了在之后的调用中,使用者能够找到它
第二个参数, 可以按照这样上面的例子这样写,因为python本身将一些浏览器实例化了, 但是还是推荐 将其赋值为 None ,因为这个参数没有更好,毕竟有些浏览器python本身并没有实例化,而这个参数也不影响它的使用
第三个参数,目前所知是浏览器的路径, 不知道有没有别的写法
当然,这里只是在这里的用法, 函数本身的意思可以去源文件中查看
下面给我一些测试的实例:
#coding:utf-8 import webbrowser as web #对导入的库进行重命名 import os import time #默认浏览器 def run_to_use_default_browser_open_url(url): web.open_new_tab(url) print 'run_to_use_default_browser_open_url open url ending ....' #firefox浏览器 def use_firefox_open_url(url): browser_path=r'C:\Program Files (x86)\Mozilla Firefox\firefox.exe' #这里的‘firefox'只是一个浏览器的代号,可以命名为自己认识的名字,只要浏览器路径正确 web.register('firefox', web.Mozilla('mozilla'), web.BackgroundBrowser(browser_path)) #web.get('firefox').open(url,new=1,autoraise=True) web.get('firefox').open_new_tab(url) print 'use_firefox_open_url open url ending ....' #谷歌浏览器 def use_chrome_open_url(url): browser_path=r'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe' web.register('chrome', None,web.BackgroundBrowser(browser_path)) web.get('chrome').open_new_tab(url) print 'use_chrome_open_url open url ending ....' #Opera浏览器 def use_opera_open_url(url): browser_path=r'C:\Program Files (x86)\Opera\launcher.exe' web.register('opera', None,web.BackgroundBrowser(browser_path)) web.get('chrome').open_new_tab(url) print 'use_opera_open_url open url ending ....' #千影浏览器 def use_qianying_open_url(url): browser_path=r'C:\Users\Administrator\AppData\Roaming\qianying\qianying.exe' web.register('qianying', None,web.BackgroundBrowser(browser_path)) web.get('qianying').open_new_tab(url) print 'use_qianying_open_url open url ending ....' #115浏览器 def use_115_open_url(url): browser_path=r'C:\Users\Administrator\AppData\Local\115Chrome\Application\115chrome.exe' web.register('115', None,web.BackgroundBrowser(browser_path)) web.get('115').open_new_tab(url) print 'use_115_open_url open url ending ....' #IE浏览器 def use_IE_open_url(url): browser_path=r'C:\Program Files (x86)\Internet Explorer\iexplore.exe' web.register('IE', None,web.BackgroundBrowser(browser_path)) web.get('IE').open_new_tab(url) print 'use_IE_open_url open url ending ....' #搜狗浏览器 def use_sougou_open_url(url): browser_path=r'D:\Program Files(x86)\SouExplorer\SogouExplorer\SogouExplorer.exe' web.register('sougou', None,web.BackgroundBrowser(browser_path)) web.get('sougou').open_new_tab(url) print 'use_sougou_open_url open url ending ....' #浏览器关闭任务 def close_broswer(): os.system('taskkill /f /IM SogouExplorer.exe') print 'kill SogouExplorer.exe' os.system('taskkill /f /IM firefox.exe') print 'kill firefox.exe' os.system('taskkill /f /IM Chrome.exe') print 'kill Chrome.exe' os.system('taskkill /f /IM launcher.exe') print 'kill launcher.exe' os.system('taskkill /f /IM qianying.exe') print 'kill qianying.exe' os.system('taskkill /f /IM 115chrome.exe') print 'kill 115chrome.exe' os.system('taskkill /f /IM iexplore.exe') print 'kill iexplore.exe' #测试运行主程序 def broswer_test(): url='https://www.baidu.com' run_to_use_default_browser_open_url(url) use_firefox_open_url(url) #use_chrome_open_url(url) use_qianying_open_url(url) use_115_open_url(url) use_IE_open_url(url) use_sougou_open_url(url) time.sleep(20)#给浏览器打开网页一些反应时间 close_broswer() if __name__ == '__main__': print ''''' ***************************************** ** Welcome to python of browser ** ** Created on 2017-05-07 ** ** @author: Jimy _Fengqi ** ***************************************** ''' broswer_test()
好了,上面的程序是测试实例, 下面对这些内容做一个整合,简化一下代码,来实现本文的根本目的
#coding:utf-8 import time import webbrowser as web import os import random #随机选择一个浏览器打开网页 def open_url_use_random_browser(): #定义要访问的地址 url='https://www.baidu.com' #定义浏览器路径 browser_paths=[r'C:\Program Files (x86)\Mozilla Firefox\firefox.exe', r'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe', r'C:\Program Files (x86)\Opera\launcher.exe', r'C:\Users\Administrator\AppData\Roaming\qianying\qianying.exe', r'C:\Users\Administrator\AppData\Local\115Chrome\Application\115chrome.exe', r'C:\Program Files (x86)\Internet Explorer\iexplore.exe', r'D:\Program Files(x86)\SouExplorer\SogouExplorer\SogouExplorer.exe' ] #选择一个浏览器 def chose_a_browser_open_url(browser_path,url): #如果传入的浏览器位置不存在,使用默认的浏览器打开 if not browser_path: print 'using default browser to open url' web.open_new_tab(url)#使用默认浏览器,就不再结束进程 else: #判断浏览器路径是否存在 if not os.path.exists(browser_path): print 'current browser path not exists,using default browser' #浏览器位置不存在就使用默认的浏览器打开 browser_path='' chose_a_browser_open_url(chose_a_browser_open_url,url) else: browser_task_name=browser_path.split('\\')[-1]#结束任务的名字 browser_name=browser_task_name.split('.')[0]#自定义的浏览器代号 print browser_name web.register(browser_name, None,web.BackgroundBrowser(browser_path)) web.get(browser_name).open_new_tab(url)#使用新注册的浏览器打开网页 print 'using %s browser open url successful' % browser_name time.sleep(5)#等待打开浏览器 kill_cmd='taskkill /f /IM '+browser_task_name#拼接结束浏览器进程的命令 os.system(kill_cmd) #终结浏览器 browser_path=random.choice(browser_paths)#随机从浏览器中选择一个路径 chose_a_browser_open_url(browser_path,url) if __name__ == '__main__': print ''''' ***************************************** ** Welcome to python of browser ** ** Created on 2017-05-07 ** ** @author: Jimy _Fengqi ** ***************************************** ''' open_url_use_random_browser()
PS:本程序在windows上面运行,python版本是2.7
相关推荐:
The above is the detailed content of Python implements randomly calling a browser to open a web page. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.
