代码拉取完成,页面将自动刷新
同步操作将从 張廣勤 势由心生/vdcode5 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:张广勤
@Web site: https://www.tunan.wang
@Github:www.github.com
@File:mybro01.py
@Time:2022/2/24 16:04
@Motto:不积跬步无以至千里,不积小流无以成江海!
"""
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 *
class Mybro:
def __init__(self):
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') # 加上不出现私密连接
self.bro = webdriver.Chrome(r'C:\Users\hsk\Downloads\chromedriver_win32 (2)/chromedriver.exe', options=opt)
self.bro.get('https://10.6.133.106/ydata/login.do')
self.bro.implicitly_wait(10)
username = self.bro.find_element_by_name("userId")
password = self.bro.find_element_by_name("passwd")
vdcode = self.bro.find_element_by_name("imgtvalidate")
vdcode_img = self.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)
self.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 = self.bro.find_element_by_xpath('/html/body/map/area[1]')
login.click()
for handle in self.bro.window_handles:
# 先切换到该窗口
self.bro.switch_to.window(handle)
# print(handle)
# 得到该窗口的标题栏字符串,判断是不是我们要操作的那个窗口
# '必应'可,'Bing'可?
if '系统错误' in self.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 self.bro.title == '系统错误':
self.bro.quit()
flag = 1
else:
flag = 0
except Exception as e:
print(e)
# bro.quit()
# flag=1
def closewindow(self):
self.bro.close()
def switchquery(self):
self.bro.switch_to.frame('olp_leftFrame')
self.bro.find_element_by_xpath('//*[@id="item6"]').click()
self.bro.find_element_by_xpath('//*[@id="M04"]/tbody/tr[1]/td[2]/a').click()
self.bro.switch_to.default_content()
def getzdopts(self):
# 右侧框架
self.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 = self.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)
# 退出框架
self.bro.switch_to.default_content()
return zdlist
def getyjnopts(self):
# 右侧框架
self.bro.switch_to.frame('olp_mainFrame')
# # 创建Select对象
# select = Select(self.bro.find_element(By.ID, "regime"))
#
# # 通过 Select 对象选中月报
# select.select_by_visible_text(zd)
# 选择月季年,得到月季年列表
yjnlist = []
yjnxz = self.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)
# 退出框架
self.bro.switch_to.default_content()
return yjnlist
def getbblist(self,zd,yjn):
# 右侧框架
self.bro.switch_to.frame('olp_mainFrame')
# 创建Select对象
select = Select(self.bro.find_element(By.ID, "regime"))
# 通过 Select 对象选中月报,zd制度
select.select_by_visible_text(zd)
# 创建Select对象
select1 = Select(self.bro.find_element(By.ID, "periodType"))
# 通过 Select 对象选中月报,yjn月季年
select1.select_by_visible_text(yjn)
time.sleep(1)
# 报表选择,得到报表列表
# table#tables td:nth-child(4)
bblist = []
bbxz = self.bro.find_elements(By.CSS_SELECTOR, 'table#tables td:nth-child(4)')
#self.bro.implicitly_wait(10)
for bb in bbxz:
# if bb.text!='':
bblist.append(bb.text)
#print(bblist)
# 报表选择器列表
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)
# 退出框架
self.bro.switch_to.default_content()
return (bbxzhb1)
def clickbb(self,bb):
# 右侧框架
self.bro.switch_to.frame('olp_mainFrame')
self.bro.find_element(By.XPATH,bb).click()
self.bro.switch_to.default_content()
#得到查询目录列表
def getquerylist(self):
# 到‘基层数据查询’窗口
for handle in self.bro.window_handles:
# 先切换到该窗口
self.bro.switch_to.window(handle)
# print(handle)
# 得到该窗口的标题栏字符串,判断是不是我们要操作的那个窗口
# '必应'可,'Bing'可?
if '基层数据查询' in self.bro.title:
# 如果是,那么这时候WebDriver对象就是对应的该窗口,正好,跳出循环,
break
# wd.title属性是当前窗口的标题栏文本
# print(bro.title)
# 记录主窗口
mainWindow = self.bro.current_window_handle
self.bro.switch_to.frame('workFrame')
# getselectoptions
chaxunmb = self.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)
self.bro.switch_to.default_content()
return cxmblist
def getbiaoti(self,query):
#切换到工作框架
self.bro.switch_to.frame('workFrame')
select = Select(self.bro.find_element(By.ID, "querytemp"))
select.select_by_visible_text(query)
# 标题
bt = self.bro.find_elements(By.CSS_SELECTOR, 'tr.tab-bt>td')
# for btx in bt:
# print(btx.text)
# "写入文件"
with open(r'data1.csv', 'w', encoding='utf-8') as f:
for btx in bt:
f.write(btx.text + ',')
f.write('\n')
# 读出修改
with open(r'data1.csv', 'r', encoding='utf-8') as f:
# str=f.read()
# print(str)
lt = f.readlines()
#print(lt[0])
#print(type(lt[0]))
lt[0] = lt[0].replace(', ,', ',')
lt[0] = lt[0].replace('操作,', '查看,往期,')
lt[0] = lt[0].replace(r'[201-1] 行业代码(GB/T4754-2017)', '行业代码')
lt[0] = lt[0].replace('期别(年)', '期别年')
lt[0] = lt[0].replace('期别(月)', '期别月')
lt[0] = lt[0].replace(r';', '')
lt[0] = lt[0].replace(r'1-本月', '本期')
# lt[0] = lt[0].replace('本期资产处置收益(损失以“-”号记)千元','本期资产处置收益千元')
lt[0] = lt[0].replace('本期资产处置收益(损失以“-”号记)千元', '本期资产处置收益千元')
lt[0] = lt[0].replace('上年同期资产处置收益(损失以“-”号记)千元', '上年同期资产处置收益千元')
# lt[0] = lt[0].replace(r'1-本月;营业收入;千元', '本期营收')
# lt[0] = lt[0].replace(r'上年同期;营业收入;千元', '上期营收')
#print(lt[0])
#print('#' * 30)
#print(lt)
# 再次写入修改好的标题行
with open(r'data1.csv', 'w', encoding='utf-8') as f:
f.writelines(lt)
# 退出框架
self.bro.switch_to.default_content()
return lt
def getdata(self):
# 切换到工作框架
self.bro.switch_to.frame('workFrame')
# 记录主窗口
mainWindow = self.bro.current_window_handle
# 修改每页30行为100行
# 占击每页30行/html/body/form/table/tbody/tr[2]/td[2]/table[2]/tbody/tr/td/a[1]
self.bro.find_element(By.XPATH, '/html/body/form/table/tbody/tr[2]/td[2]/table[2]/tbody/tr/td/a[1]').click()
# 退出当前frame
self.bro.switch_to.default_content()
# 跳入新窗口
for handle in self.bro.window_handles:
# 先切换到该窗口
self.bro.switch_to.window(handle)
# print(handle)
# 得到该窗口的标题栏字符串,判断是不是我们要操作的那个窗口
# '必应'可,'Bing'可?
if ' ' in self.bro.title:
# 如果是,那么这时候WebDriver对象就是对应的该窗口,正好,跳出循环,
break
# 输入100//*[@id="pageSize"]
self.bro.find_element(By.XPATH, '//*[@id="pageSize"]').send_keys('100')
# 点击确定/html/body/div/span[1]/input
#time.sleep(3)
self.bro.find_element(By.XPATH, '/html/body/div/span[1]/input').click()
# 回到主窗口
self.bro.switch_to.window(mainWindow)
# 回到workframe工作框架
self.bro.switch_to.frame('workFrame')
# ys 月数
n = '2021'
ys = [2]
for y in ys:
ny = n + '年' + str(y) + '月'
select = Select(self.bro.find_element(By.NAME, "period"))
# 通过 Select 对象选中
select.select_by_visible_text(ny)
time.sleep(1)
# 30行数据
datas = self.bro.find_elements(By.CSS_SELECTOR, 'tr.tab-bt~tr')
for data in datas:
print(data.text)
total_page =self.bro.find_element(By.ID, 'total_page')
print(total_page.text)
total_page = int(total_page.text[1:-1])
# total_num=int(bro.find_element(By.ID,'total_num').text)
# total_page=math.ceil(total_num/100)
# print(total_page)
page = 1
for page in range(1, total_page + 1):
# 退格符,两次,出现两次
self.bro.find_element(By.XPATH,
'/html/body/form/table/tbody/tr[2]/td[2]/table[2]/tbody/tr/td/input').send_keys('\b\b')
self.bro.find_element(By.XPATH,
'/html/body/form/table/tbody/tr[2]/td[2]/table[2]/tbody/tr/td/input').send_keys(page)
self.bro.find_element(By.XPATH, '/html/body/form/table/tbody/tr[2]/td[2]/table[2]/tbody/tr/td/a[2]').click()
trs = self.bro.find_elements(By.CSS_SELECTOR, 'tr.tab-bt~tr')
for tr in trs:
print(tr.text)
with open(r'data1.csv', 'a', encoding='utf-8') as f:
for tr in trs:
tds = tr.find_elements(By.TAG_NAME, 'td')
for td in tds:
f.write(td.text + ',')
f.write('\n')
# 退出框架
self.bro.switch_to.default_content()
#切换到---统计联网直报平台窗口
def oldwindow(self):
# 获取当前窗口句柄(窗口A)
handle = self.bro.current_window_handle
# 获取当前所有窗口句柄(窗口A、B)
handles = self.bro.window_handles
# print(handles)
# 对窗口进行遍历
for newhandle in handles:
# 筛选新打开的窗口B
if newhandle == handle:
# 切换到新打开的窗口B
self.bro.switch_to.window(newhandle)
# 在新打开的窗口B中操作
# browser.find_element_by_id('xx').click()
# 关闭当前窗口B
self.bro.close()
# 切换回窗口A
self.bro.switch_to.window(handles[0])
#print('go to oldwindow')
def getquery(self,zd,yjn):
bblist=self.getbblist(zd,yjn)
for bb in bblist:
print(bb[0])
self.clickbb(bb[1])
# 得到查询列表
cxmllist = self.getquerylist()
# print(cxmllist)
#cxmllist[0] = bb[0]
print(cxmllist)
self.oldwindow()
bro=Mybro()
bro.switchquery()
#得到制度列表
zdlist=bro.getzdopts()
print(zdlist)
#得到报表周期列表
yjnlist=bro.getyjnopts()
print(yjnlist)
#得到报表列表
#//*[@id="tables"]/tbody/tr[2]/td[4]/a,23----21年一套表制度,0---月报
# bblist=bro.getbblist(zdlist[23],yjnlist[0])
# print(bblist)
# print(bblist[0][0])
bro.getquery(zdlist[23],yjnlist[0])
#XPATH对象,点击报表,5---财务报表
# for bb in bblist:
# print(bb[0])
# bro.clickbb(bb[1])
# #得到查询列表
# cxmllist=bro.getquerylist()
# #print(cxmllist)
# cxmllist[0]=bb[0]
# print(cxmllist)
# bro.oldwindow()
# lt=bro.getbiaoti(cxmllist[1])
# print(lt)
# bro.getdata()
#bro.oldwindow()
bro.closewindow()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。