简体   繁体   English

如何使用python的默认浏览器打开和关闭网站

[英]How to open and close a website using default browser with python

I'm trying to write a python script on windows platform to open a webpage(such as Google), and then, after 10 seconds, close this website.我正在尝试在windows平台上编写一个python脚本来打开一个网页(比如谷歌),然后,10秒后,关闭这个网站。

Note: I'm using Windows 7, Python 2.7.10, and IE注意:我使用的是 Windows 7、Python 2.7.10 和 IE

You can use pythons built in webbrowser module to open the default browser:您可以使用 webbrowser 模块中内置的 python 打开默认浏览器:

import webbrowser
webbrowser.open("http://google.co.uk")

https://docs.python.org/2/library/webbrowser.html https://docs.python.org/2/library/webbrowser.html


If you want more control of the browser (for example the ability to close the browser) you could investigate the use of Selenium , however I believe you have to be specific about what browser to open.如果您想要更多地控制浏览器(例如关闭浏览器的能力),您可以调查Selenium的使用,但是我相信您必须具体说明要打开的浏览器。

from selenium import webdriver
from time import sleep

driver = webdriver.Firefox()
driver.get("http://google.co.uk")
sleep(10)
driver.close()

http://selenium-python.readthedocs.org/en/latest/getting-started.html http://selenium-python.readthedocs.org/en/latest/getting-started.html

create a subprocess and then close using the process handle创建一个子进程,然后使用进程句柄关闭

import time
import subprocess

p = subprocess.Popen(["firefox", "http://www.google.com"])
time.sleep(10) #delay of 10 seconds
p.kill()

The Best place to start web interaction in python is Mechanize .在 Python 中开始 Web 交互的最佳位置是 Mechanize。

import mechanize
br = mechanize.Browser()
br.open("http://www.example.com/")

or you can use urllib https://docs.python.org/2/howto/urllib2.html或者你可以使用 urllib https://docs.python.org/2/howto/urllib2.html

import urllib2
page = urllib2.urlopen("http://example.com/").read()
print page

i am working on ubuntu 16.04 and i solve this problem by using geckodriver.exe file.我正在 ubuntu 16.04 上工作,我通过使用 geckodriver.exe 文件解决了这个问题。 these steps are very easy,please read carefully.这些步骤非常简单,请仔细阅读。

:: at first you have to install selenium by typing this command on terminal>> :: 首先你必须通过在终端上输入这个命令来安装 selenium>>

        for python2:-  python -m pip install --user selenium
        for python3:-  python3 -m pip install --user selenium

:: next step download geckodriver using link given below >> :: 下一步使用下面给出的链接下载 geckodriver >>

       https://github.com/mozilla/geckodriver/releases

:: since i am using ubuntu so i download geckodriver-v0.24.0-linux64.tar.gz :: 因为我使用的是 ubuntu,所以我下载了geckodriver-v0.24.0-linux64.tar.gz
now extract it.现在提取它。

:: now in the python code for firefox browsing add these lines >> :: 现在在用于 Firefox 浏览的 python 代码中添加这些行 >>

 from selenium import webdriver

 browser = webdriver.Firefox(executable_path = '/home/aman/Downloads/geckodriver')
 browser.get('https://www.google.com')
 browser.close()

::for chrome browser >> ::Chrome 浏览器 >>

 from selenium import webdriver
 browser = webdriver.chrome(executable_path = '/home/aman/Downloads/geckodriver')
 browser.get('https://www.google.com')
 browser.close()

:: in my pc i extract geckodriver in /home/aman/Downloads/geckodriver so you have to put whole path for geckodriver file where you extract your file. :: 在我的电脑中,我将 geckodriver 提取到/home/aman/Downloads/geckodriver 中,因此您必须将 geckodriver 文件的整个路径放在提取文件的位置。

:: now run this python file, i hope this will definitely work for you. :: 现在运行这个 python 文件,我希望这对你有用。

Maybe try this:也许试试这个:

import time
import webbrowser
import os

url = 'https://i.pinimg.com/originals/66/b5/5f/66b55f8e2ca22a800af0aecf9d01d848.gif'

def function(test):
    x = webbrowser.open(url)

    while x != 6:
        x = webbrowser.open(url)       
        time.sleep(2)
        os.system('taskkill /im chrome.exe /f')

function('test')

I only know about opening URL(s) with Python...我只知道用 Python 打开 URL ......

See the following code:-请参阅以下代码:-

import webbrowser
enter_url=input('Enter URL you want to open: ')
webbrowser.open(enter_url)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM