Pandas, Selenium и Python более одной цены за товар для продажи - PullRequest
0 голосов
/ 06 августа 2020

Я использую Windows 10 ОС и pycharm IDE. Я пытаюсь сделать для каждого элемента на этой странице получить цены в соответствии с указанным весом, иначе добавить «0». Например, первый предмет имеет два веса 1/2 г и 1 г стоимостью 29 и 49 долларов соответственно. Таким образом, он должен записать 29 долларов за 1/2 грамма, 49 долларов за 1 грамм и 0 за восемь, четверть, половину, 1 унцию и каждый. Я разместил свой код ниже. В настоящее время мой код для первого элемента будет записывать 29 долларов и 0 за 1 / 2g. Что делать?

# 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)]
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...