I have a html scraper which pulls titles of items and prices from a website. Once in a while i want to run this scraper to update my prices, doing so i also wish to keep the old ones.
my csv where i save the titles and prices in for the first time looks like this:
Title1, price1, 'END'
Title2, price2, 'END'
Title3, price3, 'END'
I compare the new price against the old with the following method:
ind = row.index('END')
lastprijs = row[ind-1]
print lastprijs
if lastprijs != prijstitel:
row.pop(ind)
row.append(prijstitel)
row.append("END")
if a value has been found (and set) i update the csv with
with open('out.csv', 'a') as out:
tester = csv.writer(out)
tester.writerow(row)
if no value has been found i update the csv with the same row:
else:
with open('out.csv', 'a') as out:
tester = csv.writer(out)
row.append("addedddd") #add a new line.
tester.writerow(row)
However the output of my csv is as following after running it:
Item1, price1, 'END'
Item2, price2, 'END'
Item3, price3, 'END'
Item1, price1, 'END'
item1, price1, 'END'
Item2, price2, 'END'
Item3, price3, 'END'
Item1, price1, 'END'
item1, price1, 'END'
Item2, price2, 'END'
Item2, price2, 'END'
Item3, price3, 'END'
Item1, price1, 'END'
And so on... How can i fix this?
** THE FULL CODE **
def updateprices(prijstitel, titelprijs):
with open('pricewatch.csv', 'r') as csvfileadjust:
filereader = csv.reader(csvfileadjust)
print titelprijs
if titelprijs == "Gembird externe Hardeschijf behuizing met USB 2.0 aansluiting":
prijstitel = 'EDITED PRIJS!'
for row in filereader:
header = row
print header
print " ---- "
if titelprijs in row:
ind = row.index('END')
lastprijs = row[ind-1]
print lastprijs
if lastprijs != prijstitel:
row.pop(ind)
row.append(prijstitel)
row.append("END")
with open('out.csv', 'a') as out:
tester = csv.writer(out)
tester.writerow(row)
else:
with open('out.csv', 'a') as out:
tester = csv.writer(out)
row.append("addedddd") #add a new line.
tester.writerow(row)
for row in filereader: print row print ----it prints multiple rows before continuing to theifstatement.. it is a weird thing tho.