Reputation: 39
Python beginner here seeking some guidance from the dev gods. I am having issues saving the output to a CSV file, the current output is not saving all the data and does not correctly count each row ID (0,1,2,3,etc) in the CSV. Any suggestions?
Current Code:
def get_length(file_path):
with open("data.csv", "r") as csvfile:
reader = csv.reader(csvfile)
reader_list = list(reader)
return len(reader_list)
def append_list(file_path,stock_status,stock_name,date_time):
fieldnames = ['id','stock status','stock name','date/time']
next_id = get_length(file_path)
with open(file_path, "a+") as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writerow({
"id": next_id,
"stock status": stock_status,
"stock name": stock_name,
"date/time": date_time,
})
def check_add_cart():
try:
browser.find_element_by_xpath('//button[normalize-space()="Add to Cart"]')
except NoSuchElementException:
return False
return True
cycle = 2
with open('url.txt', 'r') as f:
url_list = [line.strip() for line in f]
for i in range(cycle):
for url in url_list:
browser.get(url)
time.sleep(6)
if check_add_cart():
append_list("data.csv", "In-Stock =)", browser.title, now.strftime('%Y-%m-%d %H:%M:%S'))
elif not check_add_cart():
append_list("data.csv", "Out-Of-Stock =(", browser.title, now.strftime('%Y-%m-%d %H:%M:%S'))
else:
print('Error')
browser.close()
Current CSV Output:
0,Out-Of-Stock =(,Sony PlayStation 5 Console,2021-06-05 17:45:01
2,Out-Of-Stock =(,Sony PlayStation 5 Console,2021-06-05 17:45:01
Desired CSV Output:
0,Out-Of-Stock =(,Sony PlayStation 5 Console,2021-06-05 17:45:00
1,In-Stock =),AMD Ryzen 5 5600x 4th Gen 6 Core 12 Threads,2021-06-05 17:45:01
2,Out-of-Stock =(,EVGA Geforce Rtx 3080 XC3,2021-06-05 17:45:02
3,Out-Of-Stock =(,Gigabyte Nvidia Geforce RTX 3080,2021-06-05 17:45:03
Upvotes: 0
Views: 464
Reputation: 4368
Why are you doing 2 cycles ?
And I think the error is that
if check_add_cart():
append_list("data.csv", "In-Stock =)", browser.title, now.strftime('%Y-%m-%d %H:%M:%S'))
elif not check_add_cart():
append_list("data.csv", "Out-Of-Stock =(", browser.title, now.strftime('%Y-%m-%d %H:%M:%S'))
else:
print('Error')
should be indented into the for url in url_list:
loop, otherwise it is only executed once per cycle, for the last url of the list.
Upvotes: 1