|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Could somebody help me out and tell me what did I do wrong that I getting this error message "IndexError: list index out of range"
#!/usr/bin/python #################################################################################################### ############# # THIS PYTHON PROGRAM SENDS THE EXPANDED USER QUERY TO YAHOO SEARCH # IT SAVES THE 5 CONSECUTIVE WEB PAGES OF SEARCH RESULTS GIVEN BY YAHOO # IT SAVES THE 100 PAGES OF THE CORRESPONDING LINK OF THE SEARCH RESULTS #################################################################################################### ############# import cgi, cgitb, os, sys, string, time, urllib cgitb.enable() list_yahoo_search_results=[] def save_pages(): # SETTING THE PROXIES proxies={'http':'http://192.168.36.204:8080/'} opener = urllib.FancyURLopener(proxies); # SAVING ALL RESULT FILES TO THE SERVER FOR FURTHER PROCESSING (INDEXING AND RANKING) WHICH ARE IN THE LIST FROM ABOVE for index1 in range(100): # REMOVING "/" FROM END OF THE URL h=list_yahoo_search_results[index1] o=len(h) o=o-1 if h[o]=='/': h=h[:-1] list_yahoo_search_results[index1]=h index_string=str(index1) # OPENING FILES TO SAVE THE YAHOO RESULTS PAGES result_name='result_yahoo'+index_string+'.html' file_wr=open(result_name,'w') make_url='http://'+list_yahoo_search_results[index1] #ADD http:// FOR MAKING IT COMPLETE URL page=opener.open(make_url) while 1: line=page.readline() if not line: break file_wr.write(line) file_wr.close() # FUNCTION FOR GETTING THE RESULTS FROM YAHOO SEARCH ENGINE CREATE LIST OF LINKS def get_searchresult_yahoo(): # LOOP FOR GETTING THE RESULTS GIVEN BY THE YAHOO SEARCH ENGINE FOR THE EXPANDED USER QUERY WITH INTERESTS for index in [1,21,41,61,81]: res_name='res_yahoo'+str(index)+'.html' res_open=open(res_name,'r') temp1=1 temp2=-2 while 1: line=res_open.readline() if not line:break else: if temp1==1: s=line.find('TOP 20') k=line.find('WEB RESULTS') if s != (-1): temp1=0 if k != (-1): temp1=0 if temp1==0: temp2=line.find('<em>') if temp2==0: f=line[4:] f=f[:-6] list_yahoo_search_results.append(f) if temp2 > 2: f=line[(temp2+4):] f=f[:-6] list_yahoo_search_results.append(f) save_pages() # FUNCTION FOR GENERATING THE PERSONALIZED WEB PAGE WITH TOP 20 RESULTS def new_personalizedsearchresult_page(user_query): display=open('Personalized.html','r') print "Content-type: text/html\n" for index in range(24): line=display.readline() print line print "<input type='text' name='text1' value=' '>" print """</td> <td align='left'><input type='submit' name='send' value='Web Search'></input></td> </tr>""" print """<tr></tr> <tr></tr> <tr> <td width='40%' align='right' > <font size='+1' color='#000080'> You can edit your profile for personalized search </font> </td> <td align='left'> <a href='profile.html'>Edit Profile</a> </td> </tr> </table> <br><br><br><b>TOP 20 RESULTS FOR """ print '<b><font color="red">  "'+user_query.upper() + '"</font><br><hr>' for index in range(20): print str(index+1)+".     " + "<a href='http://" + list_yahoo_search_results[index] +"'> "+list_yahoo_search_results[index]+"</a><br><br>" print """<hr><table width='100%'> <tr> <td align='center'><font color='red'>Copyright: Vikas Anand</td> </tr> </body> </html>""" # FUNCTION FOR GETTING THE YAHOO SEARCH ENGINE RESULTS FOR 5 SUCCESSIVE PAGES def send_query_to_yahoo(user_query): # MAKING QUERY IN USABLE FORM if user_query[0]=='+': user_query=user_query[1:] # SETTING THE PROXIES proxies={'http':'http://192.168.36.204:8080/'} opener = urllib.FancyURLopener(proxies); # LOOP FOR SAVING 5 SUCCESSIVE YAHOO SEARCH RESULTS PAGES for index in [1,21,41,61,81]: index_string=str(index) url='http://search.yahoo.com/search?ei=UTF-8&p='+ user_query + '&pstart=1&fr=sfp&b=' +index_string page=opener.open(url) file_name='res_yahoo'+index_string+'.html' file_write=open(file_name,'w') while 1: line=page.readline() if not line: break file_write.write(line) file_write.close() get_searchresult_yahoo() # CALL FUNCTION TO SAVE THE RESULT PAGES new_personalizedsearchresult_page(user_query) # CALL FUNCTION FOR OUTPUT # FUNCTION FOR ADDING THE USER INTERESTS TO THE USER QUERY def add_interest_to_query(user_query): interest_file="interest" # MAKING USER QUERY PRESENTABLE FOR SENDING IT TO YAHOO SEARCH ENGINE str=user_query.split(" ") s="" for i in str: s=s+'+'+i s=s[1:] user_query=s # CHECKING THE EXISTENCE OF THE USER INTERESTS' FILE (IF IT EXISTS THEN ADDING THE INTERESTS TO USER QUERY ELSE SENDING USER QUERY ONLY) if(os.path.isfile(interest_file)): # if user interest file exists interest = open(interest_file, 'r') user_interest = interest.readline() user_query_expanded = user_query + "+" + user_interest send_query_to_yahoo(user_query_expanded) else: # if user interest file does not exists send_query_to_yahoo(user_query) # FUNCTION TO BE CALLED IF NO QUERY HAS BEEN GIVEN def error_function(): print """Content-type: text/html\n <h3>You have not given any Query</h3> """ if __name__ == '__main__': sys.stderr = sys.stdout form = cgi.FieldStorage() for item in sys.argv: print item user_query=form.getfirst("user_query",sys.argv[1]).upper() if user_query == " ": error_function() else: add_interest_to_query(user_query) |
|
#2
|
||||
|
||||
|
You will need to repost this message using code tags (please read the sticky msgs at the top of the forum.
Post the full error message - it should have a complete trace and tell you exactly what line of the code caused the problem. grim ![]()
__________________
*** Experimental Python Markup CGI V2 *** |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > index error:list index out of range |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|