如何从具有相同父节点的2个不同亚马逊页面获取ASINs XPATH?

时间:2018-05-03 10:38:14

标签: selenium selenium-webdriver xpath web-scraping webdriver

我使用python和webdriver创建了一个Web抓取程序,我想从2个不同的页面中提取ASIN。我希望xpath能够同时为这两个链接工作。 这些是亚马逊页面:https://www.amazon.com/Hydro-Flask-Wide-Mouth-Flip/dp/B01ACATW7E/ref=sr_1_3?s=kitchen&ie=UTF8&qid=1520348607&sr=1-3&keywords=-gfdshttps://www.amazon.com/Ubbi-Saving-Special-Required-Locking/dp/B00821FLSU/ref=sr_1_1?s=baby-products&ie=UTF8&qid=1520265799&sr=1-1&keywords=-hgfd&th=1。它们具有相同的父节点(id,类)。如何让这个程序同时适用于这两个链接?

问题在于这些代码:36,41

asin = driver.find_element_by_xpath('//div[@id="detail-bullets"]/table/tbody/tr/td/div/ul/li[4]').text 

asin = driver.find_element_by_xpath('//div[@id="detail-bullets_feature_div"]/div[@id="detail-bullets"]/table/tbody/tr/td/div/ul/li[5]').text. I have to change these lines to output in the csv the ASINs for these 2 products. For the first link it prints the wrong information and for the second it prints the ASIN.

我附上了代码。我将不胜感激。



from selenium import webdriver
import csv
import io

# set the proxies to hide actual IP

proxies = {
    'http': 'http://5.189.133.231:80',
    'https': 'https://27.111.43.178:8080'
}

chrome_options = webdriver.ChromeOptions()

chrome_options.add_argument('--proxy-server="%s"' % ';'.join(['%s=%s' % (k, v) for k, v in proxies.items()]))

driver = webdriver.Chrome(executable_path="C:\\Users\Andrei-PC\Downloads\webdriver\chromedriver.exe",
                          chrome_options=chrome_options)
header = ['Product title', 'ASIN']

with open('csv/bot_1.csv', "w") as output:
    writer = csv.writer(output)
    writer.writerow(header)

links=['https://www.amazon.com/Hydro-Flask-Wide-Mouth-Flip/dp/B01ACATW7E/ref=sr_1_3?s=kitchen&ie=UTF8&qid=1520348607&sr=1-3&keywords=-gfds',
       'https://www.amazon.com/Ubbi-Saving-Special-Required-Locking/dp/B00821FLSU/ref=sr_1_1?s=baby-products&ie=UTF8&qid=1520265799&sr=1-1&keywords=-hgfd&th=1'
       ]

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:
        asin = driver.find_element_by_xpath('//div[@id="detail-bullets"]/table/tbody/tr/td/div/ul/li[4]').text
    except:
        print('no ASIN template one')

    try:
        asin = driver.find_element_by_xpath('//div[@id="detail-bullets_feature_div"]/div[@id="detail-bullets"]/table/tbody/tr/td/div/ul/li[5]').text
    except:
        print('no ASIN template two')

    try:
        data = [prod_title[0], asin]
    except:
        print('no items v3 ')

    with io.open('csv/bot_1.csv', "a", newline="", encoding="utf-8") as output:
        writer = csv.writer(output)
        writer.writerow(data)




1 个答案:

答案 0 :(得分:1)

您只需使用

即可
//li[b="ASIN:"]

在两个页面上获取所需元素

相关问题