代码拉取完成,页面将自动刷新
#遇到异常重新执行,没有异常正常执行下一句
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
import time
import math
from PIL import Image
from PIL import ImageGrab
from cjy import *
from listhebing import *
flag=1
while flag:
try:
time_start = time.time()
opt = Options()
opt.add_experimental_option('excludeSwitches', ['enable-automation']) # 关闭自动化提示
opt.add_argument("--start-maximized") # 最大化
opt.add_argument('ignore-certificate-errors') # 加上不出现私密连接
bro = webdriver.Chrome(r'C:\Users\hsk\Downloads\chromedriver_win32 (2)/chromedriver.exe', options=opt)
bro.get('https://10.6.133.106/ydata/login.do')
bro.implicitly_wait(10)
username = bro.find_element_by_name("userId")
password = bro.find_element_by_name("passwd")
vdcode = bro.find_element_by_name("imgtvalidate")
vdcode_img = bro.find_element_by_xpath('//*[@id="img"]')
# 用户名
username.send_keys('140700zgq')
# 密码
password.send_keys('lwzb*123!')
# 验证码图片
location = vdcode_img.location
size = vdcode_img.size
print(location, size)
box = (location['x'], location['y'] + 70, location['x'] + size['width'], location['y'] + 70 + size['height'])
print(box)
bro.save_screenshot('c:\\users\\hsk\desktop\\vdcode_3.png')
img = Image.open('c:\\users\\hsk\desktop\\vdcode_3.png')
vdcode_path = 'c:\\users\\hsk\\desktop\\vdcode_4.png'
frame = ImageGrab.grab(box)
frame.save(vdcode_path)
chaojiying = Chaojiying_Client('zgq001', 'zgq071726', '924951')
im = open(vdcode_path, 'rb').read()
result = chaojiying.PostPic(im, 1004)['pic_str']
print(result)
vdcode.send_keys(result)
login = bro.find_element_by_xpath('/html/body/map/area[1]')
login.click()
for handle in bro.window_handles:
# 先切换到该窗口
bro.switch_to.window(handle)
# print(handle)
# 得到该窗口的标题栏字符串,判断是不是我们要操作的那个窗口
# '必应'可,'Bing'可?
if '系统错误' in bro.title:
# 如果是,那么这时候WebDriver对象就是对应的该窗口,正好,跳出循环,
break
# wd.title属性是当前窗口的标题栏文本
#print(bro.title)
#bro.find_element(By.XPATH,'//*[@id="aback"]/img').click()
#a=3/0
#//*[@id="aback"]/img
if bro.title=='系统错误':
bro.quit()
flag=1
else:
flag=0
except Exception as e:
print(e)
#bro.quit()
#flag=1
'''
# 获取当前窗口句柄(窗口A)
handle = bro.current_window_handle
print(handle)'''
#左侧框架
bro.switch_to.frame('olp_leftFrame')
bro.find_element_by_xpath('//*[@id="item6"]').click()
bro.find_element_by_xpath('//*[@id="M04"]/tbody/tr[1]/td[2]/a').click()
bro.switch_to.default_content()
#右侧框架
bro.switch_to.frame('olp_mainFrame')
#bro.find_element_by_xpath('//*[@id="tables"]/tbody/tr[21]/td[3]/a').click()
#bro.find_element(By.ID,'periodType')
#选择制度,得到制度列表
zdlist=[]
zdxz=bro.find_element(By.ID,'regime')
options_zdlist=zdxz.find_elements(By.TAG_NAME,'option')
for option in options_zdlist:
zdlist.append(option.text)
print(zdlist)
#选择月季年,得到月季年列表
yjnlist = []
yjnxz=bro.find_element(By.ID,'periodType')
options_yjnlist=yjnxz.find_elements(By.TAG_NAME,'option')
for option in options_yjnlist:
yjnlist.append(option.text)
print(yjnlist)
#报表选择,得到报表列表
#table#tables td:nth-child(4)
bblist=[]
bbxz=bro.find_elements(By.CSS_SELECTOR,'table#tables td:nth-child(4)')
for bb in bbxz:
#if bb.text!='':
bblist.append(bb.text)
print(bblist)
#//*[@id="tables"]/tbody/tr[1]/td[4]/a
#//*[@id="tables"]/tbody/tr[2]/td[4]/a
#字符串构造
#报表选择器列表
bbxzqlist=[]
for i in range(1,len(bbxz)+1):
strgz='//*[@id="tables"]/tbody/tr['+str(i)+']/td[4]/a'
bbxzqlist.append(strgz)
print(bbxzqlist)
#报表选择合并
bbxzhb=hblst2(bblist,bbxzqlist)
print(bbxzhb)
#删除空项,列表中没有显示的项
bbxzhb1=[]
for i in range(len(bbxzhb)):
if bbxzhb[i][0]!='':
bbxzhb1.append(bbxzhb[i])
print(bbxzhb1)
#退出框架
bro.switch_to.default_content()
i=0
for item in bbxzhb1:
# 获取当前窗口句柄(窗口A)
handle = bro.current_window_handle
#print(handle)
i+=1
#print(item[0],item[1])
print('第%s次运行***********************'%i)
# 切换进入框架
bro.switch_to.frame('olp_mainFrame')
# 创建Select对象
select = Select(bro.find_element(By.ID, "periodType"))
# 通过 Select 对象选中月报
select.select_by_visible_text("年报")
#点击服务业月报//*[@id="tables"]/tbody/tr[23]/td[4]/a
#//*[@id="tables"]/tbody/tr[1]/td[4]/a
#//*[@id="tables"]/tbody/tr[2]/td[4]/a
bro.find_element(By.XPATH,item[1]).click()
#退出框架
bro.switch_to.default_content()
#到‘基层数据查询’窗口
for handle in bro.window_handles:
# 先切换到该窗口
bro.switch_to.window(handle)
#print(handle)
# 得到该窗口的标题栏字符串,判断是不是我们要操作的那个窗口
#'必应'可,'Bing'可?
if '基层数据查询' in bro.title:
# 如果是,那么这时候WebDriver对象就是对应的该窗口,正好,跳出循环,
break
# wd.title属性是当前窗口的标题栏文本
#print(bro.title)
#记录主窗口
mainWindow = bro.current_window_handle
bro.switch_to.frame('workFrame')
#getselectoptions
chaxunmb=bro.find_element(By.ID,'querytemp')
cxmblist=[]
options_list = chaxunmb.find_elements(By.TAG_NAME,'option')
for option in options_list:
#print("value is:%s text is %s"%(option.get_attribute('value'),option.text))
cxmblist.append(option.text)
print(cxmblist)
select = Select(bro.find_element(By.ID, "querytemp"))
# 通过 Select 对象选中,有可能为空,选默认'选择查询模板'
if len(cxmblist)==1:
select.select_by_visible_text(cxmblist[0])
else:
select.select_by_visible_text(cxmblist[1])
#退出框架
bro.switch_to.default_content()
bro.switch_to.window(mainWindow)
#print(bro.title)
# 获取当前所有窗口句柄(窗口A、B)
handles = bro.window_handles
#print(handles)
# 对窗口进行遍历
for newhandle in handles:
# 筛选新打开的窗口B
if newhandle==handle:
# 切换到新打开的窗口B
bro.switch_to.window(newhandle)
# 在新打开的窗口B中操作
# browser.find_element_by_id('xx').click()
# 关闭当前窗口B
bro.close()
# 切换回窗口A
bro.switch_to.window(handles[0])
#print(bro.title)
#bro.quit()
'''
flag=1
while flag:
try:
a=3/1
print('kkkkk')
print('ppppp')
flag=0
except Exception as e:
if e:
print(e)
flag=1
print("next")
'''
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。