我有一个代码,可以输出任何给定链接的所有可能的下拉名称(例如颜色),值(例如红色)和价格。我想将此输出写入特定标题下的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标题下,我想要颜色等。我不想使用任何其他格式,因为我将抓取许多网址,并且下拉标题每次都不同。
尽管内容输出正在,
[{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(T恤)尺寸较小