【发布时间】:2018-11-06 16:19:34
【问题描述】:
我用python写了一个脚本来获取网站上的产品价格,但是我发现了一个问题。有时有些产品在打折,因为它们有 2 个价格(原始价格和实际价格)。我的脚本得到了所有这些,但我不想要销售前的价格。我怎样才能排除它们?有可能吗?
源代码示例:
正常价格
<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>
我的脚本
import csv
from urllib.request import urlopen
from bs4 import BeautifulSoup
site = input()
html = urlopen(site)
bs = BeautifulSoup(html, 'html.parser')
pricesList = bs.findAll('div',{'class':'result-actions'})
csvFile = open('Prices.csv', 'wt+')
writer = csv.writer(csvFile)
try:
for prices in pricesList:
print(clean_up_text(prices.get_text()))
csvPrice = []
csvPrice.append(clean_up_text(prices.get_text().strip()))
writer.writerow(csvPrice)
finally:
csvFile.close()
请帮帮我!
更新
我试图包含一个功能来排除旧价格,但它似乎也不起作用。
def excluir_precos_antigos(element):
element = driver.find_element_by_class_name('price-before')
driver.execute_script("""var element =
arguments[0];element.parentNode.removeChild(element);""", element)
【问题讨论】:
标签: python csv beautifulsoup