Selenium записывает вывод в CSV, используя импорт CSV и Python - PullRequest
0 голосов
/ 27 мая 2019

У меня есть код, который выводит все возможные имена выпадающих меню (например, цвет), значения (например, красный) и цену для любой ссылки.Я хочу записать этот вывод в CSV-файл под определенными заголовками.

from selenium import webdriver
import csv
browser = webdriver.Chrome(executable_path='C:\Users\user\PycharmProjects\seleniumTest\drivers\chromedriver.exe')
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
from selenium.webdriver.support.select import Select

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

import csv



from selenium.webdriver.support.select import Select
from selenium.webdriver import Chrome
from selenium.common.exceptions import NoSuchElementException

import itertools
from pprint import pformat


def apply_values(dropdowns, values):
    """
    :param dropdowns: list of select DropDown
    :param values: list of values to set
    :return: dict with key=dropdownName, value=dropDownValue
    """
    r = {}
    for value in values:
        # For each value, get the associated DropDown and set it
        for dropdown in dropdowns:
            if value in dropdown.values:
                try:
                    dropdown.select_by_visible_text(value)
                    r[dropdown.name] = value
                except NoSuchElementException:
                    # print("Unable to set the following values {}..Skipping".format(values))
                    # This is caused by an option not being available with other selected dropdown values
                    # You can also check the attribute disabled for this
                    return False
                break
    return r

browser.get('https://www.ebay.co.uk/itm/Apple-iPhone-7-32GB-128GB-256GB-All-Colours-UNLOCKED-Various-Grades/133027295946?')
els = browser.find_elements_by_css_selector(".msku-sel")
selects = []
for el in els:
    # Adding the dropdown name to the select object
    name = el.get_attribute('name')
    select = Select(el)
    select.name = name
    selects.append(select)

print("Get all options for each dropdown")
for idx, sel in enumerate(selects):
    sel.values = [option.text for option in sel.options][1:]

print("Get all possible permutations")
permutations = list(itertools.product(*[sel.values for sel in selects]))

# Iteration for all possible permutation
print("Select all possible permutation and get price for each")
results = []
for permutation in permutations:
    # Resetting all parameter to default
    for sel in selects:
        sel.select_by_index(0)
    # Iteration to set each dropdown
    result = apply_values(selects, permutation)
    if result:
        # Once all dropdown value are set, get the finally price
        result['Price'] = browser.find_element_by_id("prcIsum").text
        results.append(result)

with open('results222.csv', 'w') as f:
    f.write("dropdown1Name,dropdown1Option,dropdown2Name,dropdown2Option,dropdown3Name,dropdown3Option,dropdown4Name,dropdown4Option,dropdown5Name,dropdown5Option,dropdown6Name,dropdown6Option \n")
    f.write(pformat(result))
    f.write(",  \n")
    f.write(result['Price'])
    f.write(",  \n")

print(pformat(results))


browser.close()

Текущий вывод csv:


dropdown1Name,dropdown1Option,dropdown2Name,dropdown2Option,dropdown3Name,dropdown3Option,dropdown4Name,dropdown4Option,dropdown5Name,dropdown5Option,dropdown6Name,dropdown6Option 
{u'Color': u'Rose Gold',
 u'Condition': u'Grade A - Very Good',
 u'Network': u'GSM Unlocked',
 'Price': u'\xa3224.99',
 u'Storage Capacity': u'128GB'},  
£224.99,  

Прежде всего, я хочу, чтобы это было выровнено, например, под заголовком dropdown1name, я хочу цвет и так далее.Я не хочу использовать какой-либо другой формат, так как я буду очищать многие URL, а выпадающий заголовок будет отличаться каждый раз.

Кроме того, он записывает только первый набор записей в CSV, несмотря на вывод содержимого


[{u'Color': u'Black',
  u'Condition': u'Grade B - Very Good',
  u'Network': u'GSM Unlocked',
  'Price': u'\xa3179.99',
  u'Storage Capacity': u'32GB'},
 {u'Color': u'Black',
  u'Condition': u'Grade A+ - Excellent',
  u'Network': u'GSM Unlocked',
  'Price': u'\xa3195.99',
  u'Storage Capacity': u'32GB'},
 {u'Color': u'Black',
  u'Condition': u'Grade A - Very Good',
  u'Network': u'GSM Unlocked',
  'Price': u'\xa3194.99',
  u'Storage Capacity': u'32GB'},
 {u'Color': u'Black',
  u'Condition': u'Grade B - Very Good',
  u'Network': u'GSM Unlocked',
  'Price': u'\xa3179.99',
  u'Storage Capacity': u'256GB [out of stock]'},
 {u'Color': u'Black',
  u'Condition': u'Grade A+ - Excellent',
  u'Network': u'GSM Unlocked',
  'Price': u'\xa3179.99',
  u'Storage Capacity': u'256GB [out of stock]'},
 {u'Color': u'Black',
  u'Condition': u'Grade A - Very Good',
  u'Network': u'GSM Unlocked',
  'Price': u'\xa3179.99',
  u'Storage Capacity': u'256GB [out of stock]'},
 {u'Color': u'Black',
  u'Condition': u'Grade B - Very Good',
  u'Network': u'GSM Unlocked',
  'Price': u'\xa3204.99',
  u'Storage Capacity': u'128GB'},
 {u'Color': u'Black',
  u'Condition': u'Grade A+ - Excellent',
  u'Network': u'GSM Unlocked',
  'Price': u'\xa3225.99',
  u'Storage Capacity': u'128GB'},
 {u'Color': u'Black',
  u'Condition': u'Grade A - Very Good',
  u'Network': u'GSM Unlocked',
  'Price': u'\xa3224.99',
  u'Storage Capacity': u'128GB'},
 {u'Color': u'Gold',
  u'Condition': u'Grade B - Very Good',
  u'Network': u'GSM Unlocked',
  'Price': u'\xa3179.99',
  u'Storage Capacity': u'32GB'},
 {u'Color': u'Gold',
  u'Condition': u'Grade A+ - Excellent',
  u'Network': u'GSM Unlocked',
  'Price': u'\xa3195.99',
  u'Storage Capacity': u'32GB'},
 {u'Color': u'Gold',
  u'Condition': u'Grade A - Very Good',
  u'Network': u'GSM Unlocked',
  'Price': u'\xa3194.99',
  u'Storage Capacity': u'32GB'},
 {u'Color': u'Gold',
  u'Condition': u'Grade B - Very Good',
  u'Network': u'GSM Unlocked',
  'Price': u'\xa3204.99',
  u'Storage Capacity': u'128GB'},
 {u'Color': u'Gold',
  u'Condition': u'Grade A+ - Excellent',
  u'Network': u'GSM Unlocked',
  'Price': u'\xa3225.99',
  u'Storage Capacity': u'128GB'},
 {u'Color': u'Gold',
  u'Condition': u'Grade A - Very Good',
  u'Network': u'GSM Unlocked',
  'Price': u'\xa3224.99',
  u'Storage Capacity': u'128GB'},
 {u'Color': u'Rose Gold',
  u'Condition': u'Grade B - Very Good',
  u'Network': u'GSM Unlocked',
  'Price': u'\xa3179.99',
  u'Storage Capacity': u'32GB'},
 {u'Color': u'Rose Gold',
  u'Condition': u'Grade A+ - Excellent',
  u'Network': u'GSM Unlocked',
  'Price': u'\xa3195.99',
  u'Storage Capacity': u'32GB'},
 {u'Color': u'Rose Gold',
  u'Condition': u'Grade A - Very Good',
  u'Network': u'GSM Unlocked',
  'Price': u'\xa3194.99',
  u'Storage Capacity': u'32GB'},
 {u'Color': u'Rose Gold',
  u'Condition': u'Grade B - Very Good',
  u'Network': u'GSM Unlocked',
  'Price': u'\xa3204.99',
  u'Storage Capacity': u'128GB'},
 {u'Color': u'Rose Gold',
  u'Condition': u'Grade A+ - Excellent',
  u'Network': u'GSM Unlocked',
  'Price': u'\xa3225.99',
  u'Storage Capacity': u'128GB'},
 {u'Color': u'Rose Gold',
  u'Condition': u'Grade A - Very Good',
  u'Network': u'GSM Unlocked',
  'Price': u'\xa3224.99',
  u'Storage Capacity': u'128GB'}]

Помощь будет высоко оценена в следующем формате (например) dropdown1name dropdown1option и т. Д. Url1 (телефон) цвет Черный размер url2 (футболка) маленький

...