I am trying to download file from the website www.nsf.gov. In the browser, first I have to make a search request.Then, I have to click on the export file option to download the file.
If, I try to do it manually,first I have to paste the url of the search request.Then,I need to paste the export url in the browser.If I do not do the first process,it gives me the following message :
Server Error
This server has encountered an internal error which prevents it from fulfilling your request. The most likely cause is a misconfiguration. Please ask the administrator to look for messages in the server's error log.
So, programatically using Webkit I do the following,but still it gives me the following error:
urllib2.HTTPError: HTTP Error 500: Server Error.
Kindly help me...I am struggling with this for a week now. Here is my code :
#!/usr/bin/env python
import sys
import signal
from optparse import OptionParser
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import QWebPage
from bs4 import BeautifulSoup
import urllib2
import shutil
import urlparse
import os
class Crawler( QWebPage ):
def __init__(self,url_name,file_name):
QWebPage.__init__( self )
self._url = url_name
self._file = file_name
def crawl( self ):
signal.signal( signal.SIGINT, signal.SIG_DFL )
self.connect( self, SIGNAL( 'loadFinished(bool)' ), self._finished_loading )
self.mainFrame().load( QUrl( self._url ) )
def _finished_loading( self, result ):
file = open( self._file, 'w' )
file.write( self.mainFrame().toHtml() )
file.close()
self.process( self.mainFrame().toHtml())
file_download('http://www.nsf.gov/awardsearch/ExportResultServlet?exportType=txt','result.txt')
sys.exit( 0 )
def process(self,content):
html_doc=content
soup = BeautifulSoup(html_doc)
soup=soup.prettify()
def main():
url_name='http://www.nsf.gov/awardsearch/advancedSearchResult?PIId=&PIFirstName=&PILastName=&PIOrganization=&PIState=&PIZip=&PICountry=&ProgOrganization=&ProgEleCode=&BooleanElement=All&ProgRefCode=&BooleanRef=All&Program=&ProgOfficer=&Keyword=&AwardNumberOperator=Range&AwardNumberFrom=1&AwardNumberTo=20000&AwardAmount=&AwardInstrument=&ActiveAwards=true&OriginalAwardDateOperator=&StartDateOperator=&ExpDateOperator='
file_name='NSF Award Search: Advanced Search Results1.html'
app = QApplication( sys.argv )
crawler = Crawler(url_name,file_name)
crawler.crawl()
sys.exit( app.exec_() )
def file_download(url, fileName):
r = urllib2.urlopen(urllib2.Request(url))
try:
fileName = fileName
with open(fileName, 'wb') as f:
shutil.copyfileobj(r,f)
finally:
r.close()
if __name__ == '__main__':
main()