How to open a web page through Python after installing python

Release: 2019-07-03 10:29:23
Original
4776 people have browsed it

How to open a web page through Python after installing python

Python中可以通过selenium webdriver和webbrowser方法打开网页。

一、selenium webdriver

1、首先需要安装selenium和webdriver

pip install selenium

2、安装相应浏览器的driver.exe,以chrome为例。下载chromedriver.exe文件,放到chrome的application目录下,与chrome.exe相同的目录,并将chromedriver.exe加到path环境变量中,也可以直接在程序中指定。

3、代码:

from selenium import webdriver
 
driver=webdriver.Chrome()
 
with open("D:\\1.txt",'r') as f:
    for line in f.readlines():
        driver.get(line) 
str = input("Enter any key to exit")
 
driver.quit()
Copy after login

注:1.txt中可以包含多个网址,每个网址单独一行。

二、webbrowser

这种方式比较简单,不需要额外安装其它模块

import webbrowser
 
chromePath='C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe'#chrome.exe文件位置
webbrowser.register("chrome", None, webbrowser.BackgroundBrowser(chromePath))#第一个参数名字随便起,只要与下面保持一致就可以了
chr = webbrowser.get("chrome")#这里的chrome就是register里的第一个参数
with open(".\\url.txt",'r') as f:
    for line in f.readlines():
        chr.open(line,new=1, autoraise=True)#这里的new autoraise好像不起作用了,无论怎么取值,总是每个网址一个新的页面打开
Copy after login

注:

1、selenium webdriver方式会打开一个全新的浏览器,不会影响机器自身的浏览器使用,也不会改变机器自身浏览器信息。

2、webbrowser打开机器自身的浏览器,操作会对机器自带浏览器有影响。

更多Python相关技术文章,请访问Python教程栏目进行学习!

The above is the detailed content of How to open a web page through Python after installing python. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template