This login script will allow you to login a website with python. After logging in, You now have access to all the pages for “members only” accessed with python and it can convert the required pages to pdf format save automatically in your /home/.
In this script i use anna university official website to get the exam result after that it convert the result page into pdf format and save to /home/.
Modules used :
urllib2
BeautifulSoup(bs4)
requests
sys
PyQt4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | from urllib2 import urlopen
from bs4 import BeautifulSoup
import requests
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
app = QApplication(sys.argv)
URL= 'http://sdpdr.nic.in/annauniv/Authentication'
#get userdata
RegisterNumber = raw_input("Enter the registration number : ")
DateofBirth = raw_input("Enter the date of birth [DD-MM-YYYY] : ")
def main():
# Start a session so we can have persistant cookies
# Session() >> http://docs.python-requests.org/en/latest/api/#request-sessions
session = requests.Session()
# This is the form data that the page sends when logging in
login_data = {
'username': RegisterNumber,
'password': DateofBirth,
'submit': 'id',
}
print login_data
# Authenticate
r = session.post(URL, data = login_data)
# Try accessing a page that requires you to be logged in
r = session.get('http://sdpdr.nic.in/annauniv/result')
web = QWebView()
web.load(QUrl("http://sdpdr.nic.in/annauniv/result"))
#web.show()
printer = QPrinter()
printer.setPageSize(QPrinter.A4)
printer.setOutputFormat(QPrinter.PdfFormat)
printer.setOutputFileName("result.pdf")
# convertion of page to pdf format
def convertIt(main):
web.print_(printer)
print "Pdf generated"
QApplication.exit()
QObject.connect(web, SIGNAL("loadFinished(bool)"), convertIt)
sys.exit(app.exec_())
if __name__ == '__main__':# main method
main()
|
for more information and output visit https://emilgeorgejames.wordpress.com/2015/07/15/python-script-to-login-to-a-website-and-convert-required-html-page-to-pdf/