from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from multiprocessing import Pool
import win32com


class steel_open(object):

    def __init__(self):
        pass

        
    def open_url(self,driver,u):
        driver.get(u)
        #driver.maximize_window()
        
        print("打开网页")
        time.sleep(2)
        
    def choose_login(self,driver,name,pwd):
        self.swith_handle(driver)
        driver.find_element_by_css_selector("#mysteel-topBar > div.login-bar > span").click()
        driver.find_element_by_css_selector("[class='userName']").send_keys(name)
        driver.find_element_by_css_selector("[class='pasd pasdPsd hide']").send_keys(pwd)
        driver.find_element_by_css_selector("[class='loginBtnBig']").click()
        time.sleep(3)
        
        print("登录")
     
    def operate_url(self,driver,steel,day):
    
        self.swith_handle(driver)
        driver.refresh()
        if steel == "镀锌":
            js="window.scrollBy(0,700)"
            driver.execute_script(js)
            driver.find_element_by_css_selector("body > div.wrap > div:nth-child(4) > div:nth-child(2) > div.section_tab_level_one.menu_hook > a.active").click()
            self.swith_handle(driver)
            driver.refresh()
            driver.find_element_by_partial_link_text(day + "乐从市场" + steel + "板卷价格行情").click()
            time.sleep(2)
        if steel == "唐山市场建筑钢材":
            label = driver.find_element_by_partial_link_text("市场行情")
            ActionChains(driver).move_to_element(label)
            driver.find_element_by_partial_link_text(day + steel + "价格行情").click()
            time.sleep(2)
        if steel == "热轧":
            m = driver.find_element_by_partial_link_text(day + "乐从市场" + steel + "板卷价格行情")
            menu = WebDriverWait(driver, 15).until(lambda driver: m)
            menu.click()
        if steel == "冷轧":
            js="window.scrollBy(0,700)"
            driver.execute_script(js)
            label = driver.find_element_by_link_text("乐从")
            ActionChains(driver).move_to_element(label).perform()
            #menu = WebDriverWait(driver=driver, timeout=15).until(EC.visibility_of_element_located((By.LINK_TEXT, ll)))
            m = driver.find_element_by_partial_link_text(day + "乐从市场" + steel + "板卷价格行情")
            menu = WebDriverWait(driver, 15).until(lambda driver: m)
            menu.click()

        print("操作网页")


    def choose_list(self,driver,lis):
        self.swith_handle(driver)
        print(1)
        i=1
        driver.find_element_by_id('specDiv').click()
        driver.find_element_by_id(lis[0]).click()
        while lis[i]:
            driver.find_element_by_id('placeDiv').click()
            driver.find_element_by_id(lis[i]).click()
            i+=1
        print("选择完毕")
    

    def steel_all(self,driver,web,name,l):
        print(web)
        driver.execute_script("window.open('')")
        self.swith_handle(driver)
        self.open_url(driver,web)
        self.operate_url(driver,name,day)
        self.choose_list(driver,l)

    def swith_handle(self,driver):
        driver.switch_to.window(driver.window_handles[-1])
                
    
if __name__ == '__main__':

    li =  ["1.0*1219*C","鞍钢","马钢","首钢"]
    li1 = ['5.5-11.5*1500*C',"鞍钢"]
    li2 = ['Ф18-25',"河钢唐钢"]
    li3 = ['1.0*1250*C',"鞍钢"]
    ali = [li,li1,li2,li3]
    url_name = ''
    url_pwd = ''
    name = "镀锌"
    name1 = "热轧"
    name2 = "唐山市场建筑钢材"
    name3 = "冷轧"
    aname = [name,name1,name2,name3]
    tuduwang ='https://tudu.mysteel.com/'
    url1 = 'https://rezha.mysteel.com/'
    url2 = 'https://tangshan.mysteel.com/' 
    url3 = 'https://lengzha.mysteel.com/'
    aurl = [tuduwang,url1,url2,url3]
    day = input("请输入查询日期:")+""
    driver = webdriver.Chrome()
    s = steel_open()
    s.open_url(driver,tuduwang)
    s.choose_login(driver,url_name,url_pwd)
    for i in range(0,4):
        try:
            s.steel_all(driver,aurl[i],aname[i],ali[i])
        except Exception as e:
            print(e)

 

posted on 2019-08-21 13:35  喻解  阅读(257)  评论(0编辑  收藏  举报