Good morning, friends. I'm having trouble removing a child. I wrote a code to collect all the prices of the products of a website (it is a list of products, not a page for each one). On this with no problems, the code works well. It turns out that sometimes some product goes on sale, and on the site there are 2 prices, the old and the new (at a discount), and my code pulls both. The old price is not interesting, so I wanted to ignore it when I'm pulling the data, but I can not make it happen. An example of the source code:
<div class="result-actions"
<span> ==$0
$ 1,98
</span>
<div class="result-actions">
<span>
<small class="price-before"> ==$0
$ 56,70
</small>
<span class="price-now">
$ 39,60
</span>
</span>
Each "result-actions" represents a product. I was suggested to pull the "price-now", but in this case the first product of the example would not be pulled by my code, since it is not on promotion and therefore does not contain the class. My code trying to delete the child, but without success:
with open('Lista.csv') as example_file:
example_reader = csv.reader(example_file)
for row in example_reader:
driver.get(row[0])
html = driver.page_source
bs = BeautifulSoup(html, 'html.parser')
precosLista = bs.findAll('div',{'class':'result-actions'})
f = open(acha_proximo_nome('Arquivo.csv'), 'wt+', newline='')
writer = csv.writer(f)
try:
for precos in precosLista:
print(precos.get_text())
csvPreco = []
csvPreco.append(clean_up_text(precos.get_text()))
js = "var aa = document.getElementsByClassName('price-before')[0];aa.parentNode.removeChild(aa)"
driver.execute_script(js)
writer.writerow(csvPreco)
finally:
f.close()
Without the
js = "var aa = document.getElementsByClassName('price-before')[0];aa.parentNode.removeChild(aa)"
driver.execute_script(js)
My code works fine, but it's how I said it, collects everything, including what I do not want. Anyone have any idea how I can fix this?