help with lists and writing to file in correct order

Kent Johnson kent at kentsjohnson.com
Fri Dec 30 09:24:29 EST 2005


homepricemaps at gmail.com wrote:
> hey mike-the sample code was very useful.  have 2 questions
> 
> when i use what you wrote which is listed below i get told
> unboundlocalerror: local variable 'product' referenced before
> assignment.  

You would get this error if you have a <tr> that doesn't have an <hr 
id="dealName">. Do you have some <tr> that are not products? If so you 
need to filter them out somehow. Or have you misspelled something? Your 
sample data has id="foodName" not "dealName".

You might do better with an incremental development. Start with
for row in bs('tr'):
   print row

and expand from there. At each step use print statements to make sure 
you are finding the data you expect.

Kent

if i however chnage row to incident in "for incident in
> bs('tr'):" i then get mytuples printed out nicely but once again get a
> long list of
> 
> [('pizza;','pizza hut;', '3.94;')]
> [('pizza;','pizza hut;', '3.94;')]
> 
> 
>  for row in bs('tr'):
>     	data=[]
>     	for incident in row('h2',  {'id' : 'dealName'}):
>         	productlist = []
> 		for oText in incident.fetchText( oRE):
>             		productlist.append(oText.strip() + ';')
> 		product = ''.join(productlist)
> 
> 	for incident in row('a',  {'name' : 'D0L3'}):
>         	storelist = []
> 		for oText in incident.fetchText( oRE):
>             		storelist.append(oText.strip() + ';')
> 		store = ''.join(storelist)
> 
>                  tuple = (product, store, price)
>                  data.append(tuple)
>                  print data
> 



More information about the Python-list mailing list