kAldown

givetwo_promo

Dec 30th, 2015
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.19 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. from selenium import webdriver
  4. import codecs
  5. import time
  6.  
  7. #### DEFINE ####
  8.  
  9. names = {
  10.          ' -167px;': 'choco',
  11.          ' -668px;': 'choco with nuts',
  12.          ' -835px;': 'orange',
  13.          ' -501px;': 'green',
  14.          ' 0px;'   : 'roasted orange',
  15.          ' -334px;': 'green hat',
  16.         }
  17.  
  18. names2 = {k:v for v,k in names.items()}
  19.  
  20. ################
  21.  
  22. promos = dict()
  23.  
  24. url = "http://www.givetwo.ru/promo/"
  25. driver = webdriver.Remote("http://localhost:4444/wd/hub", desired_capabilities=webdriver.DesiredCapabilities.HTMLUNITWITHJS)
  26. #driver = webdriver.Firefox()
  27. driver.get(url)
  28. element = driver.find_element_by_xpath("/html/body/div[1]/div[6]/div/div[7]/div[2]/a")
  29.  
  30. def push():
  31.     element.click()
  32.     time.sleep(5)
  33.     m1_check = driver.find_element_by_xpath("/html/body/div[1]/div[6]/div/div[3]/div")
  34.     m2_check = driver.find_element_by_xpath("/html/body/div[1]/div[6]/div/div[4]/div")
  35.     m3_check = driver.find_element_by_xpath("/html/body/div[1]/div[6]/div/div[5]/div")
  36.  
  37.     m1_style = m1_check.get_attribute("style").split(":")[1]
  38.     m2_style = m2_check.get_attribute("style").split(":")[1]
  39.     m3_style = m3_check.get_attribute("style").split(":")[1]
  40.  
  41.     if m1_style == m2_style and m1_style == m3_style:
  42.         return m1_style, driver.find_element_by_xpath("/html/body/div[1]/div[6]/div/div[7]/div[1]/strong").text
  43.     else:
  44.         #return None
  45.         print m1_style, m2_style, m3_style
  46.  
  47. def find_as_much(how_much):
  48.     assert how_much <= 6
  49.     while len(promos) != how_much:
  50.         res = push()
  51.         if res != None:
  52.             key = names[res[0]]
  53.             promos[key] = res[1]
  54.  
  55.     with codecs.open('find_as_much.txt', 'a', encoding='utf8') as f:
  56.         for k, v in promos.items():
  57.             f.write('{0}\{1}'.format(str(k), str(v)))
  58.  
  59. def find_certain(what_to_find):
  60.     assert isinstance('what_to_find', str)
  61.     what_to_find = names2[what_to_find]
  62.     while True:
  63.         res = push()
  64.         if res[0] == what_to_find:
  65.             with codecs.open('{0}.txt'.format(what_to_find), 'a', encoding='utf8') as f:
  66.                 f.write('{0}'.format(res[1]))
  67.  
  68. #find_as_much(6)
  69. [push() for x in range(10)]
  70.  
  71. driver.quit()
Advertisement
Add Comment
Please, Sign In to add comment