国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python實(shí)現(xiàn)隨機(jī)調(diào)用一個(gè)瀏覽器打開網(wǎng)頁

2020-02-22 23:47:59
字體:
供稿:網(wǎng)友

前兩天總結(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()		            
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 车险| 定州市| 英德市| 贵南县| 银川市| 中宁县| 岳阳市| 南岸区| 南川市| 建德市| 吉木乃县| 太仓市| 金沙县| 大庆市| 嫩江县| 克山县| 泗洪县| 多伦县| 收藏| 台山市| 稷山县| 同德县| 安乡县| 湘西| 樟树市| 榕江县| 林口县| 宁海县| 邓州市| 原阳县| 隆昌县| 旺苍县| 句容市| 外汇| 裕民县| 分宜县| 仙桃市| 汉中市| 缙云县| 江都市| 台东市|