码迷,mamicode.com
首页 > 其他好文 > 详细

简单的混合驱动

时间:2019-01-23 17:24:08      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:coding   action   spl   data   cut   __name__   数据文件   url   strip()   

数据文件:

testdata.txt

visit||http://www.bing.com
visit||${e:\urls.txt}

urls.txt
http://www.sohu.com
http://www.sogou.com
http://www.baidu.com

#encoding=utf-8
from selenium import webdriver
import re

driver = webdriver.Chrome(executable_path="d:\\chromedriver")

def visit(url):
    global driver
    driver.get(url)

def main(filepath):
    with open(filepath) as fp:
        for line in fp:
            if line.strip():
                if re.search(r"\${(.*?)}",line):
                    action = line.split("||")[0]
                    data_file = re.search(r"\${(.*?)}",line).group(1)
                    with open(data_file)  as file_obj:
                        for url in  file_obj:
                            command = "%s(‘%s‘)" %(action,url.strip())
                            eval(command)

                else:
                    action = line.split("||")[0]
                    url = line.split("||")[1].strip()
                    command = "%s(‘%s‘)" %(action,url)
                    eval(command)

if __name__ == "__main__":
    main("E:\\python\\自动化\\testdata.txt")
    driver.quit()

简单的混合驱动

标签:coding   action   spl   data   cut   __name__   数据文件   url   strip()   

原文地址:http://blog.51cto.com/13496943/2345880

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!