Python implementation randomly calls a browser to open a Web page

Source: Internet
Author: User
Below for everyone to share a Python implementation of random call a browser to open the Web page, with a good reference value, I hope to help you. Come and see it together.

Two days ago summed up the Python crawler two ways to open a Web page using a real browser summary

But that's just a summary of it, today this article to practice a practical walkthrough

Still using the WebBrowser module to invoke the browser

About three ways to open in the previous article has been said, here no longer repeat

If you do not specifically register, you will be using the default browser to open the Web page, as follows:

#默认浏览器 #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 .... '

A non-default browser is really registered:

The Firfox browser used here first

#firefox浏览器 def use_firefox_open_url (URL):  browser_path=r ' C:\Program Files (x86) \mozilla Firefox\firefox.exe '  #这里的 ' Firefox ' is just a browser code name that you can name as you know, as long as the browser path is correct  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 .... '

Explain the current usage of this registration function

Web.register () It's three parameters

The first name for the browser to be re-ordered, the main purpose is to be in the subsequent call, the user can find it

The second argument, which can be written in the example above, because Python itself instantiates some browsers, but it is recommended to assign to None, because this parameter is not better, after all, some browser python itself is not instantiated, And this parameter doesn't affect its use.

The third parameter, which is now known as the path of the browser, does not know if there is any other notation

Of course, here is just the usage here, the function itself meaning can be viewed in the source file

Here are some examples of my tests:

#coding: Utf-8import WebBrowser as Web #对导入的库进行重命名import osimport time# default browser 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 ' is just a browser code name, can be named for their own known names, as long as the browser path correctly web.register (' Firefox ', the 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, None,web. Backgroundbrowser (Browser_path)) web.get (' + '). 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 .... Sogou browser 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 ' Ki ll 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 () #给浏览器打开网页一些反应时间close_broswer () if __name__ = = ' __main__ ': print ' * * * Welcome to Python of browser * *    * * Created on 2017-05-07 * * * @author: Jimy _fengqi * * ***************************************** "bros Wer_test ()

OK, the above program is a test instance, the following to do a consolidation of these content, simplify the code, to achieve the fundamental purpose of this article

#coding: Utf-8import timeimport WebBrowser as Webimport osimport random# randomly select a browser to open the page 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 the incoming browser location does not exist, use the default browser to open the If not Browser_path:print ' using the default browser to open URL ' web.open_new_tab (URL) #使用默认浏览器, Do not end the process else: #判断浏览器路径是否存在if not os.path.exists (browser_path):p rint ' 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_nameweb.register (Browser_name, None,web. Backgroundbrowser (Browser_path)) Web.get (browser_name). Open_new_tab (URL) #使用新注册的浏览器打开网页print ' using%s browser open URL successful '% Browser_nametime.sleep (5) #等待打开浏览器kill_cmd = ' taskkill/f/im ' +browser_task_name# Stitching End Browser Process command Os.system (kill_cmd) #终结浏览器browser_path =random.choice (browser_paths) #随机从浏览器中选择一个路径chose_a_browser_ Open_url (browser_path,url) if __name__ = = ' __main__ ': print ' "***************************************** * * Welcom E to Python of browser * * * * Created on 2017-05-07 * * * @author: Jimy _fengqi * * ************************ "' Open_url_use_random_browser ()

PS: This program runs on Windows, Python version is 2.7

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.