from selenium import webdriver
from selenium.webdriver.common.by import By
import time
import ddddocr
def get_zhongxin():
# #这个是一个用来控制chrome以无界面模式打开的浏览器
# #创建一个参数对象,用来控制chrome以无界面的方式打开
# chrome_options = Options()
# #后面的两个是固定写法 必须这么写
# chrome_options.add_argument('--headless')
# chrome_options.add_argument('--disable-gpu')
# #驱动路径 谷歌的驱动存放路径
# path = 'chromedriver\\chromedriver.exe'
# #创建浏览器对象
# driver5 = webdriver.Chrome(executable_path=path,chrome_options=chrome_options)
# s = requests.Session()
# fb = webdriver.FirefoxProfile.DEFAULT_PREFERENCES
#driver5 = webdriver.Firefox()
# op = webdriver.FirefoxOptions()
# op.set_preference({'user-data-dir':''})
c = webdriver.ChromeOptions()
c.add_argument('--user-data-dir=D:/AutomationProfile')
#c.add_argument('--headless')
# c.add_argument(f'user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36')
driver5 = webdriver.Chrome()
# #driver5.get("https://www.citicsf.com/e-futures/")
#driver5.get('https://www.citicsf.com/e-futures/user/login')
driver5.get('https://www.citicsf.com/e-futures/research/more?code=000112')
time.sleep(3)
try:
driver5.find_element(By.XPATH, '//div[@class="input-group"]/input').send_keys('')#这里直接指向id="title"的同级input就行。
time.sleep(1)
driver5.find_element(By.XPATH, '//div[@class="form-group"]/div[2]/input').send_keys('')#这里直接指向id="title"的同级input就行。
time.sleep(1)
image = driver5.find_element(By.XPATH, '//div[@class="form-group"]/div[3]/span/img[@id="kaptcha"]')
print("image",image)
data = image.screenshot_as_base64
print("data_______________",data)
ocr = ddddocr.DdddOcr()
# 进行验证码识别
img_base64=data
ocr = ocr.classification(img_base64) # img_bytes=data 这是bytes数据传入时,但在pycharm 会冒黄,我也不清楚为啥
print(ocr)
driver5.find_element(By.XPATH, '//div[@class="form-group"]/div[3]/input').send_keys(ocr)#这里直接指向id="title"的同级input就行。
driver5.find_element(By.XPATH, '//div[@class="content"]//button').click()#这里直接指向id="title"的同级input就行。
time.sleep(4)
currenturl = driver5.current_url
while currenturl=="https://www.citicsf.com/e-futures/user/login":
time.sleep(1)
driver5.find_element(By.XPATH, '//div[@class="layui-layer-btn"]/a').click()
image = driver5.find_element(By.XPATH, '//div[@class="form-group"]/div[3]/span/img[@id="kaptcha"]')
print("image",image)
data = image.screenshot_as_base64
print("data_______________",data)
ocr = ddddocr.DdddOcr()
# 进行验证码识别
img_base64=data
ocr = ocr.classification(img_base64) # img_bytes=data 这是bytes数据传入时,但在pycharm 会冒黄,我也不清楚为啥
print(ocr)
driver5.find_element(By.XPATH, '//div[@class="form-group"]/div[3]/input').send_keys(ocr)#这里直接指向id="title"的同级input就行。
driver5.find_element(By.XPATH, '//div[@class="content"]//button').click()#这里直接指向id="title"的同级input就行。
time.sleep(4)
currenturl = driver5.current_url
driver5.find_element(By.XPATH, '//span[@class="nyhg-icon"]/a').click()
time.sleep(1)
driver5.find_element(By.XPATH, '//li[@data-code="000112"]//span[@data-icon="icon-minus-sign"]/a').click()
time.sleep(1)
driver5.find_element(By.XPATH, '//li[@data-code="00011101"]/a[@class="toggleLeaf"]').click()
time.sleep(1)
driver5.find_element(By.XPATH, '//li[@class="article-list"]/ul/li/a[contains(text(),"中信期货原油")]').click()
except:
pass
get_zhongxin()
如果你那边可以运行,可能是网络的问题了 |