如何打印标题,没有价格'并且“购物车中没有商品'对于购物车中没有商品的商品?

时间:2018-03-19 11:28:24

标签: python web-scraping webdriver amazon

我使用网络驱动程序制作了一个python程序。我想打印标题,打印'没有价格'并且“购物车中没有商品'对于第二个链接(谁没有购物车中的商品)。 在这个程序中,我使用不同的try和except块来表示不同的模板。我在列表中放了2个链接(最重要的是在购物车中没有产品的第二个链接) 如何将上述信息输出到第二个链接的csv?我附上了代码。我将不胜感激任何帮助。只需运行该程序,您就会看到我想要的东西。

from selenium import webdriver
from time import sleep
from selenium.webdriver.common.keys import Keys
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', 'Items in cart']

with open('csv/products.csv', "w") as output:
    writer = csv.writer(output)
    writer.writerow(header)
links = ['https://www.amazon.com/Doris-Home-Vertical-Invitations-Rhinestone/dp/B01K48E5NA/ref=sr_1_2?s=home-garden&ie=UTF8&qid=1520358436&sr=1-2&keywords=-hgfd&th=1',
         'https://www.amazon.com/Sunglasses-Necklaces-Bracelets-Temporary-Accessories/dp/B073RP2SX9/ref=sr_1_4?s=toys-and-games&ie=UTF8&qid=1520358554&sr=1-4&keywords=-hgfd']

for i in range(len(links)):
    driver.get(links[i])
    try:
        product_title = driver.find_elements_by_xpath('//*[@id="productTitle"][1]')
        try:
            prod_price = driver.find_element_by_xpath('//span[@id="priceblock_ourprice"]').text
        except:
            print('no price v1')
        try:
            prod_price = driver.find_element_by_xpath('(//span[@id="_price"]/span)[4]').text
        except:
            print('no price v2')
        prod_title = [x.text for x in product_title]
        try:
            driver.execute_script("window.scrollTo(0,200)")
            sizemenu = driver.find_element_by_id('dropdown_selected_size_name')
            sizemenu.click()
            select = driver.find_element_by_id('size_name_1')  # medium size
            select.click()
        except:
            print('no select')

        sleep(3)
        driver.execute_script("window.scrollTo(0,50)")

        try:
            driver.execute_script("window.scrollTo(0,600)")
            driver.find_element_by_xpath('//*[@id="a-autoid-5"]/span/input').click()
        except:
            print('no add to cart button one')
        try:
            driver.execute_script("document.getElementById('buybox-see-all-buying-choices-announce').click()")
        except:
            print('no add to cart button two')
        try:
            driver.find_element_by_xpath('//*[@id="a-autoid-14"]/span/input').click()
        except:
            print('no add to cart button three v14')
        try:
            driver.find_element_by_xpath('//*[@id="a-autoid-15"]/span/input').click()
        except:
            print('no add to cart button three v15')
        try:
            driver.find_element_by_xpath('//*[@id="a-autoid-0"]/span/input').click()
        except:
            print('no add to cart button three v0')
        try:
            driver.find_element_by_xpath('//*[@id="a-autoid-1"]/span/input').click()
        except:
            print('no add to cart button three v1')
        try:
            driver.execute_script("document.getElementById('add-to-cart-button').click()")
        except:
            print('no add to cart button four')
        try:
            driver.execute_script("document.getElementById('siNoCoverage-announce').click()")
        except:
            print('no add to cart button five')
        try:
            driver.find_element_by_xpath('//*[@id="submit.add-to-cart"]/span/input').click()
        except:
            print('no add to cart button six')
        sleep(3)
        try:
            driver.execute_script("document.getElementById('smartShelfAddToCartNative').click()")
        except:
            print('no continue button')

        try:
            driver.find_element_by_xpath('//*[@id="hlb-view-cart"]/span/a').click()
        except:
            print('no cart button')

        sleep(3)

        try:

            driver.find_element_by_xpath('//*[@id="a-autoid-0-announce"]/span[2]').click()
            driver.find_element_by_xpath('//*[@id="dropdown1_9"]').click()
            quantity_xpath = '//*[@id="activeCartViewForm"]/div[2]/div/div[4]/div/div[3]/div/div/input'
            quantity_el = driver.find_element_by_xpath(quantity_xpath)
            quantity_el.send_keys("999" + Keys.ENTER)
        except:
            print('No itrack')
        sleep(2)
        try:
            items_cart = driver.find_elements_by_xpath('//div[@class="a-alert-content"]/span')
            items_in_cart = [x.text for x in items_cart]
            items_in_cart=', '.join(items_in_cart)
            csvfile = 'csv/products.csv'

            data = [prod_title[0], prod_price,items_in_cart]

            with open(csvfile, "a", newline="") as output:
                writer = csv.writer(output)
                writer.writerow(data)
        except:
            print('No items v1')
    except:
        product_title = driver.find_element_by_xpath('//*[@id="productTitle"][1]').text()
        prod_price = 'No price'
        items_in_cart = 'No items in cart'
        csvfile = 'csv/products.csv'
        data = [product_title, prod_price, items_in_cart]

        with open(csvfile, "a", newline="") as output:
            writer = csv.writer(output)
            writer.writerow(data)

0 个答案:

没有答案