【Python】混合驱动实例

keywords2.txt:
get||ie||{urls.txt}
get||chrome||http://www.iciba.com

main.py:
from selenium import webdriver
import time
import re

def get(browser_type,url,i):
    if  browser_type.lower()=="chrome":
        driver=webdriver.Chrome(executable_path="c:\\chromedriver")
    elif browser_type.lower()=="ie":
        driver=webdriver.Ie(executable_path="c:\\IEDriverServer")
    else:
        driver=webdriver.Firefox(executable_path="c:\\geckodriver")
    driver.get(url)
    #截屏保存
    screenpic=driver.get_screenshot_as_file(r"e:\\test4\\"+"screenPic"+str(i)+".png")
    time.sleep(3)
    driver.quit()

with open("keywords2.txt") as f:
    for line in f:
        action,browser_type,url=tuple(line.strip().split("||"))
        if re.search(r"{(.*)}",url):
            file_name=re.search(r"{(.*)}",url).group(1)
            with open(file_name) as fp:
                i=0
                for url in fp:
                    command=action+"('"+browser_type+"','"+url.strip()+"','"+str(i)+"')"
                    #print command
                    i+=1
                    try:
                        exec(command)
                    except Exception,e:
                        print e
        else:
            i=3
            command=action+"('"+browser_type+"','"+url.strip()+"','"+str(i)+"')"
            try:
                exec(command)
            except Exception,e:
                print e

猜你喜欢

转载自www.cnblogs.com/jingsheng99/p/9147540.html