前兩天總結(jié)了一下python爬蟲 使用真實(shí)瀏覽器打開網(wǎng)頁的兩種方法總結(jié)
但那僅僅是總結(jié)一下而已,今天本文來實(shí)戰(zhàn)演練一下
依然使用的是 webbrowser 這個(gè)模塊 來調(diào)用瀏覽器
關(guān)于的三種打開方式在上一篇文章中已經(jīng)說過了,這里不再贅述
如果沒有特意注冊(cè),那么將會(huì)是使用默認(rèn)的瀏覽器來打開網(wǎng)頁,如下:
#默認(rèn)瀏覽器 #coding:utf-8 import webbrowser as web #對(duì)導(dǎo)入的庫進(jìn)行重命名 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 ....'
真正的注冊(cè)一個(gè)非默認(rèn)瀏覽器:
這里先用的firfox瀏覽器
#firefox瀏覽器 def use_firefox_open_url(url): browser_path=r'C:/Program Files (x86)/Mozilla Firefox/firefox.exe' #這里的‘firefox'只是一個(gè)瀏覽器的代號(hào),可以命名為自己認(rèn)識(shí)的名字,只要瀏覽器路徑正確 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 ....' 解釋一下這個(gè)注冊(cè)函數(shù)當(dāng)前的用法
web.register() 它的三個(gè)參數(shù)
第一個(gè)為 自己給瀏覽器重新命的名字, 主要目的是為了在之后的調(diào)用中,使用者能夠找到它
第二個(gè)參數(shù), 可以按照這樣上面的例子這樣寫,因?yàn)閜ython本身將一些瀏覽器實(shí)例化了, 但是還是推薦 將其賦值為 None ,因?yàn)檫@個(gè)參數(shù)沒有更好,畢竟有些瀏覽器python本身并沒有實(shí)例化,而這個(gè)參數(shù)也不影響它的使用
第三個(gè)參數(shù),目前所知是瀏覽器的路徑, 不知道有沒有別的寫法
當(dāng)然,這里只是在這里的用法, 函數(shù)本身的意思可以去源文件中查看
下面給我一些測(cè)試的實(shí)例:
#coding:utf-8import webbrowser as web #對(duì)導(dǎo)入的庫進(jìn)行重命名import osimport time#默認(rèn)瀏覽器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'只是一個(gè)瀏覽器的代號(hào),可以命名為自己認(rèn)識(shí)的名字,只要瀏覽器路徑正確 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 ....' #瀏覽器關(guān)閉任務(wù) 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' #測(cè)試運(yùn)行主程序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)#給瀏覽器打開網(wǎng)頁一些反應(yīng)時(shí)間 close_broswer()if __name__ == '__main__': print ''''' ***************************************** ** Welcome to python of browser ** ** Created on 2017-05-07 ** ** @author: Jimy _Fengqi ** ***************************************** ''' broswer_test()
新聞熱點(diǎn)
疑難解答
圖片精選