How to write a python script to continue a webpage until it opens

We are waiting for the result of my sister. And, as it happens, a lot, with the Indian government. server is slow, traffic is heavy.

So, I thought about writing a python program to keep trying until the server responds to the Http request. But the program:

import urllib i=1 f = open("C:/Users/DELL/Desktop/neetpg.html",'w') while(True): try: page = urllib.urlopen("http://www.nbe.gov.in/asr/neet_pdf/") print "Done" break except: print i i += 1 continue f.write(page.read()) print "check" 

But the program does not work properly. I tried replacing url with facebook.com , it still prints numbers.

In addition, I want this to be done if the server responds, the web page loads the js and css files along with the html file, and all this should open in a browser.

I also made a tip http://docs.python.org/2/library/webbrowser.html and changed the program to:

 import webbrowser i=1 while(True): try: webbrowser.open("http://www.nbe.gov.in/asr/neet_pdf/") print "Done" break except: print i i += 1 continue print "check" 

But all this does, opens a new window in my browser by default and sets the URL for what is given, and "opens it." Meanwhile, printing Done and Check on the python shell.

A web browser that does not receive a response from the server displays could not connect to www.nbe.gov.in

How to do it?

EDIT: Just saw that the facebook.com script worked in the end. It took about 15 attempts, and then it happened. The .html file is .html correctly. With all the CSS and probably JS too.

Why is it that it took so many attempts, while I can just easily open facebook.com from a browser.

+4
source share
1 answer

Give selenium .

The idea is to keep opening the page until the driver sees the correct title. And if it is there, just break the loop and leave the page open:

 from selenium import webdriver driver = webdriver.Firefox() while True: driver.get("http://www.nbe.gov.in/asr/neet_pdf/") if 'NEET-PG' in driver.title: break 

Hope this helps.

+2
source

Source: https://habr.com/ru/post/1481260/


All Articles