The following is a summary of two methods of using a python crawler to open a web page using a real browser. It has a good reference value and I hope it will be helpful to everyone. Let’s take a look together
1. Use the system’s own library os
The advantage of this method is that any browser can use it , The disadvantage is that you cannot open web pages one after another freely
import os os.system('"C:/Program Files/Internet Explorer/iexplore.exe" http://www.baidu.com')
2. Use the python integrated library webbroswer
Python’s webbrowser module supports some operations on the browser. There are mainly three methods:
import webbrowser webbrowser.open(url, new=0, autoraise=True) webbrowser.open_new(url) webbrowser.open_new_tab(url)
Although they are three methods, they are actually one method, but the parameters passed are different
webbrowser.open(url, new=0, autoraise=True) in the system's default browser Access the url address. If new=0, the url will be opened in the same
browser window; if new=1, a new browser window will be opened; new=2, the new browser tab will be opened. Open
The other two functions webbrowser.open_new(url) and webbrowser.open_new_tab(url) actually return webbrowser.open() This method
It’s just that they don’t have the other two parameters.
You need to register in advance when calling other browsers, otherwise the default browser will open the page
import webbrowser chromePath = r'你的浏览器目录' # 例如我的:D:\Google\Chrome\Application\chrome.exe webbrowser.register('chrome', None, webbrowser.BackgroundBrowser(chromePath)) #这里的'chrome'可以用其它任意名字,如chrome111,这里将想打开的浏览器保存到'chrome' webbrowser.get('chrome').open('www.baidu.com',new=1,autoraise=True)
webbrowser The browser types supported by this library lock are as follows:
Type Name Class Name Notes 'mozilla' Mozilla('mozilla') 'firefox' Mozilla('mozilla') 'netscape' Mozilla('netscape') 'galeon' Galeon('galeon') 'epiphany' Galeon('epiphany') 'skipstone' BackgroundBrowser('skipstone') 'kfmclient' Konqueror() (1) 'konqueror' Konqueror() (1) 'kfm' Konqueror() (1) 'mosaic' BackgroundBrowser('mosaic') 'opera' Opera() 'grail' Grail() 'links' GenericBrowser('links') 'elinks' Elinks('elinks') 'lynx' GenericBrowser('lynx') 'w3m' GenericBrowser('w3m') 'windows-default' WindowsDefault (2) 'macosx' MacOSX('default') (3) 'safari' MacOSX('safari') (3) 'google-chrome' Chrome('google-chrome') 'chrome' Chrome('chrome') 'chromium' Chromium('chromium') 'chromium-browser' Chromium('chromium-browser')
These contents can be seen in the source file, the path is : python2.7/libs/webbroser.py
Related recommendations:
How to use Python crawler to get those valuable blog posts
Record a simple Python crawler instance
The above is the detailed content of Summary of two methods of using Python crawler to open web pages using real browsers. For more information, please follow other related articles on the PHP Chinese website!