This script loops at the "while True:" written to scrape data from several pages by clicking the next button at the bottom, but I cannot figure out how to structure the code to continue writing to the HTML as it paginates. Instead, it overwrites the html results written previously. Your help is appreciated. Thanks!
while True:
time.sleep(10)
golds = driver.find_elements_by_css_selector(".widgetContainer #widgetContent > div.singleCell")
print("found %d golds" % len(golds))
template = """\
<tr class="border">
<td class="image"><img src="{0}"></td>\
<td class="title"><a href="{1}" target="_new">{2}</a></td>\
<td class="price">{3}</td>
</tr>"""
lines = []
for gold in golds:
goldInfo = {}
goldInfo['title'] = gold.find_element_by_css_selector('#dealTitle > span').text
goldInfo['link'] = gold.find_element_by_css_selector('#dealTitle').get_attribute('href')
goldInfo['image'] = gold.find_element_by_css_selector('#dealImage img').get_attribute('src')
try:
goldInfo['price'] = gold.find_element_by_css_selector('.priceBlock > span').text
except NoSuchElementException:
goldInfo['price'] = 'No price display'
line = template.format(goldInfo['image'], goldInfo['link'], goldInfo['title'], goldInfo['price'])
lines.append(line)
try:
#clicks next button
driver.find_element_by_link_text("Next→").click()
except NoSuchElementException:
break
time.sleep(10)
html = """\
<html>
<body>
<table>
<tr class='headers'>
<td class='image'></td>
<td class='title'>Product</td>
<td class='price'>Price / Deal</td>
</tr>
</table>
<table class='data'>
{0}
</table>
</body>
</html>\
"""
f = open('./result.html', 'w')
f.write(html.format('\n'.join(lines)))
f.close()