【发布时间】:2018-03-16 00:03:47
【问题描述】:
我使用 xpath 和 web 驱动程序为 2 个不同的链接制作了一个 python 程序。我想获得带有 2 个 ID 的价格。该程序从 2 个不同的页面运行,这就是价格有 2 个 ID 的原因。我使用了 try 和 except 但它不起作用。我附上了代码。 现在我得到 IndexError: list index out of range。我将不胜感激。如果你想问我任何问题。
from selenium import webdriver
import csv
# set the proxies to hide actual IP
proxies = {
'http': 'http://218.50.2.102:8080',
'https': 'http://185.93.3.123:8080',
}
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--proxy-server=%s' % proxies)
driver = webdriver.Chrome(executable_path="C:\\Users\Andrei\Downloads\chromedriver_win32\chromedriver.exe",
chrome_options=chrome_options)
header = ['Product title', 'Product price']
with open('csv/products.csv', "w") as output:
writer = csv.writer(output)
writer.writerow(header)
links = ['https://www.amazon.com/Windsor-Glider-Ottoman-White-Cushion/dp/B017XRDV5S/ref=sr_1_1?s=home-garden&ie=UTF8&qid=1520265105&sr=1-1&keywords=-gggg&th=1',
'https://www.amazon.com/Instant-Pot-Multi-Use-Programmable-Packaging/dp/B00FLYWNYQ/ref=sr_1_1?s=home-garden&ie=UTF8&qid=1520264922&sr=1-1&keywords=-gggh']
for i in range(len(links)):
driver.get(links[i])
product_title = driver.find_elements_by_xpath('//*[@id="productTitle"][1]')
prod_title = [x.text for x in product_title]
try:
product_price = driver.find_elements_by_xpath('//*[@id="priceblock_ourprice"][1]')
prod_price = [x.text for x in product_price]
except:
print('no price v1')
try:
product_price = driver.find_elements_by_xpath('//*[@id="_price"][1]')
prod_price = [x.text for x in product_price]
except:
print('no price v2')
csvfile = 'csv/products.csv'
data = [prod_title[0], prod_price[0]]
with open(csvfile, "a", newline="") as output:
writer = csv.writer(output)
writer.writerow(data)
【问题讨论】:
-
请改进您的question
-
只要运行代码,你就会明白我的意思
-
考虑搜索您感兴趣的特定标签:
prod_price = driver.find_element_by_xpath('//span[@id='priceblock_ourprice']').text -
我使用了 prod_price = driver.find_element_by_xpath('//span[@id="priceblock_ourprice"]').text 并且我得到了相同的 IndexError: list index out of range。可以请你运行程序看看吗?
-
第二个链接如何使用 driver.find_element_by_xpath?