熊猫,硒和蟒蛇每件出售的价格超过一个

时间:2020-08-06 08:32:06

标签: python pandas selenium

我正在使用Windows 10 OS和pycharm IDE。我要尝试的是为此page上的每个商品获得列出的权重价格,否则添加“ 0”。例如,第一件商品有两个重量1 / 2g和1g,分别价值$ 29和$ 49。因此,1 / 2g的价格应为29美元,1g的价格为49美元,八,四分之一,一半,1盎司及每盎司的价格为0。我已经在下面发布了我的代码。目前,我的第一项代码会记录为$ 29,而1 / 2g记录为0。我该怎么办?

# Import libraries
import time
from selenium import webdriver
import pandas as pd

# Open the google chrome browser and navigate to website
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(executable_path=PATH)

# Total amount of pages to web scrape
MAX_PAGE_NUM = 1

# Iterate through the pages and web scrape
for i in range(1, MAX_PAGE_NUM + 1):
    url = "https://weedmaps.com/deliveries/callweed-2?page=" + str(2)
    driver.get(url)
    print(url)

    # Find the elements by using xpath
    product_name = driver.find_elements_by_xpath('//div[@class ="styled-components__Name-sc-186ferk-9 lbdlVL"]')
    product_type = driver.find_elements_by_xpath('//span[@class ="styled-components__BrandCategory-sc-186ferk-6 dTklNg"]')
    prices = driver.find_elements_by_xpath('//span[@class="styled-components__Price-sc-6ubro-3 dEwZFC"]')
    weight = driver.find_elements_by_xpath('//div[@class="src__Box-sc-1sbtrzs-0 src__Flex-sc-1sbtrzs-1 styled-components__UnitLabel-sc-6ubro-1 dGTmZk"]')

    # Get the length of the products
    num_page_items = len(product_name)
    num_prices = len(prices)


    # Store the product names and the types in respective lists
    products = [product_name[j].text for j in range(num_page_items)]
    types = [product_type[j].text for j in range(num_page_items)]
    weights = [weight[k].text for k in range(num_prices)]


    # Append prices to respective lists
    halfg = [prices[k].text if weight[k].text == '1/2 g' else '0' for k in range(num_prices)]
    oneg = [prices[k].text if weight[k].text == '1 g' else '0' for k in range(num_prices)]
    eighth = [prices[k].text if weight[k].text == '1/8 oz' else '0' for k in range(num_prices)]
    quarter = [prices[k].text if weight[k].text == '1/4 oz' else '0' for k in range(num_prices)]
    halfoz = [prices[k].text if weight[k].text == '1/2 oz' else '0' for k in range(num_prices)]
    oneoz = [prices[k].text if weight[k].text == '1 oz' else '0' for k in range(num_prices)]
    each = [prices[k].text if weight[k].text == 'each' else '0' for k in range(num_prices)]

0 个答案:

没有答案