简体   繁体   中英

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.

Note: I'm using Windows 7, Python 2.7.10, and IE

You can use pythons built in webbrowser module to open the default browser:

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

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.

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

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 .

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

or you can use 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. these steps are very easy,please read carefully.

:: at first you have to install selenium by typing this command on terminal>>

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

:: next step download geckodriver using link given below >>

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

:: since i am using ubuntu so i download geckodriver-v0.24.0-linux64.tar.gz
now extract it.

:: now in the python code for firefox browsing add these lines >>

 from selenium import webdriver

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

::for chrome browser >>

 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.

:: now run this python file, i hope this will definitely work for you.

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...

See the following code:-

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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