1 Star 0 Fork 0

abc-edb-fund/edb_mapping_nbs

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
target_indicator_search.py 7.31 KB
一键复制 编辑 原始数据 按行查看 历史
李新 提交于 2018-07-16 18:41 . Initial Commit
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018/7/16 18:40
# @Author : Orient
# @File : target_indicator_search.py
# @Software: PyCharm
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from config import conn_data_center,MongoConnCT,province,city,dict_region,dict_symbol
import time
# 模型名称:农业:主要农作物播种面积
# 头部关键词、指标单位和指标频率:播种面积,千公顷,年
url = 'http://114.55.107.24:8088/index'
usr = 'lidongfang'
pwd = '123456'
modelurl = 'http://114.55.107.24:8088/model/manage'
#创建session
my_conn = MongoConnCT()
cursor = conn_data_center()
title = input('模型名称:')
keyword_head, indicator_unit, indicator_frequency = input("头部关键词、指标单位和指标频率:").split(',')
#提取来源指标名
class getResourceIndicator():
def getDataRaw(self):
table = my_conn.db['edb_mapping_table'].find_one({"data_source": "国家统计局", "title": title},{"_id": 0, "title": 1, "data": 1})
resource_indicators = []
for item in table['data']:
# 写入来源指标名称
if item['column'] == 0 and item['row'] > 1 :
resource_indicators.append(item['text'])
return resource_indicators
#解析来源指标名
def ResourceIndicatorPrase(self):
resource_indicators = self.getDataRaw()
region = province() + city()
indicator_prase = []
#设置指标特征标记初始值
for resource_indicator in resource_indicators:
dict = {}
dict['resource_indicator_name'] = resource_indicator
dict['keyword_head'] = keyword_head
dict['keyword_tail'] = ''
dict['region'] = ''
dict['indicator_unit'] = indicator_unit
dict['indicator_frequency'] = indicator_frequency
#提取region
for item in region:
if item in resource_indicator:
dict['region'] = item
#提取keyword_tail
keyword_tail = resource_indicator.replace(keyword_head, '')
keyword_tail = keyword_tail.replace(dict['region'], '')
keyword_tail = keyword_tail.replace(indicator_unit, '')
keyword_tail = keyword_tail.replace(indicator_frequency, '')
for item in dict_symbol.items():
keyword_tail = keyword_tail.replace(item[0], item[1])
for item in dict_region.items():
keyword_tail = keyword_tail.replace(item[0], item[1])
dict['keyword_tail'] = keyword_tail
indicator_prase.append(dict)
return indicator_prase
def getTargetID(self):
indicator_parse = self.ResourceIndicatorPrase()
target_indicator = []
for item in indicator_parse:
keyword_head = item['keyword_head'] + '%'
keyword_tail = '%' + item['keyword_tail'] + '%'
region = '%' + item['region'] + '%'
unit = '%' + item['indicator_unit'] + '%'
frequency = '%' + item['indicator_frequency'] + '%'
sql = '''
select id,indicator_name from edb_indicator_value
where indicator_name like "%s"
and indicator_name like "%s"
and indicator_name like "%s"
and indicator_unit like "%s"
and indicator_frequency like "%s"
order by id
''' % (keyword_head, keyword_tail, region, unit, frequency)
result = cursor.execute(sql)
row = cursor.fetchall()
target = {}
target['resource_indicator_name'] = item['resource_indicator_name']
if result == 1:
target['target_id'] = row[0][0]
target['target_name'] = row[0][1]
else:
target['target_id'] = ''
target['target_name'] = ''
target_indicator.append(target)
return target_indicator
def isElementExist(self,xpath):
global driver
try:
driver.find_element_by_xpath(xpath)
return True
except:
return False
# 登录模型管理界面
def login(self):
global driver
driver = webdriver.Firefox()
driver.get(url)
driver.find_element_by_xpath('//form/input[1]').clear()
driver.find_element_by_xpath('//form/input[1]').send_keys(usr)
driver.find_element_by_xpath('//form/input[2]').clear()
driver.find_element_by_xpath('//form/input[2]').send_keys(pwd)
driver.find_element_by_xpath('//*[@id="login"]').click()
WebDriverWait(driver, 20, 0.5).until(EC.presence_of_element_located((By.CLASS_NAME, 'J_menuItem')))
driver.get(modelurl)
return driver
# 从配置文件读取目标指标ID到自生产系统
def uploadTargetID(self):
target_indicator = self.getTargetID()
driver = self.login()
# 读取模型名称
driver.find_element_by_xpath('//*[@id="searchName"]').clear()
driver.find_element_by_xpath('//*[@id="searchName"]').send_keys(title)
time.sleep(2)
driver.find_element_by_xpath('//*[@class = "btn btn-success"]').click()
xpath = '//table/tbody/tr/td[8]/a[2]'
if self.isElementExist(xpath):
time.sleep(2)
driver.find_element_by_xpath(xpath).click()
time.sleep(10)
driver.find_element_by_xpath('/html/body/div[1]/div[1]/div/div/div[1]/div/div[1]/a').click()
driver.find_element_by_xpath('/html/body/div[1]/div[1]/div/div/div[1]/div/div[2]/a[1]').click()
time.sleep(3)
# 查找目标指标个数
resources = driver.find_elements_by_xpath('//div[@class="table-box input"]')
targets = driver.find_elements_by_xpath("//div[@class='targetIndexId indicator_mapping_starter']")
n = len(targets)
# 清空来源指标名
jsClear = '$(arguments[0]).empty()'
driver.execute_script(jsClear, resources)
# 填写指标编辑
for i in range(n):
# 填写来源指标名
resource = resources[i]
jsResource = '$(arguments[0]).text("%s")'%target_indicator[i]['resource_indicator_name']
driver.execute_script(jsResource, resource)
# 填写目标指标名
target = targets[i]
id = target_indicator[i]['target_id']
name = target_indicator[i]['target_name']
if isinstance(id, int):
sub = '%s[%s]' % (name, id)
js0 = '$(arguments[0]).attr("data-id", %s)' % id
js1 = '$(arguments[0]).attr("data-title","%s")' % name
js2 = '$(arguments[0]).text("%s")' % sub
driver.execute_script(js0, target)
driver.execute_script(js1, target)
driver.execute_script(js2, target)
else:
pass
else:
print('该模型未找到')
return False
# 保存草稿
#driver.find_element_by_xpath('//*[@id="saveDraft"]').click()
return driver
test = getResourceIndicator()
test.uploadTargetID()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/edbmapping/edb_mapping_nbs.git
git@gitee.com:edbmapping/edb_mapping_nbs.git
edbmapping
edb_mapping_nbs
edb_mapping_nbs
master

搜索帮助