@archlinux
import requests
from selenium.webdriver import Chrome
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from time import sleep
import schedule
import time
import datetime
import csv
def upload_cookie():
try:
web = Chrome()
web.implicitly_wait(10)
web.get(url='https://www.joinquant.com/user/login/index?type=login')
web.find_element(By.XPATH, '/html/body/div[2]/div/div/ul[2]/li[1]/div[1]/form/input[1]').send_keys(
"账号")
web.find_element(By.XPATH, '/html/body/div[2]/div/div/ul[2]/li[1]/div[1]/form/input[2]').send_keys('密码')
web.find_element(By.XPATH, '/html/body/div[2]/div/div/ul[2]/li[1]/div[1]/form/button').click()
sleep(3)
web.find_element(By.XPATH, '/html/body/section/div/ul/li[2]/a/span').click()
sleep(0.5)
web.find_element(By.XPATH, '/html/body/section/div/ul/li[2]/div/div/a[1]').click()
sleep(3)
web.switch_to.frame('research')
web.find_element(By.XPATH, '//*[@id="notebook_list_info"]/span/input').send_keys(
r'C:\Users\Administrator\PycharmProjects\pythonProject\实战目录\爬虫\cookie.txt')
sleep(1)
web.find_element(By.XPATH, '//*[@id="notebook_list"]/div[2]/div/div/button[1]').click()
sleep(1)
web.find_element(By.XPATH, '/html/body/div[5]/div/div/div[3]/button[2]').click()
except Exception as e:
print(f"上传cookie失败: {e}")
def download_cookie():
try:
web=Chrome()
web.implicitly_wait(10)
web.get(url='https://m.touker.com/trading/trade/position')
web.find_element(By.XPATH,'//*[@id="app"]/div/div[1]/form/div[1]/div/div/div/div/input').send_keys('证券账户')
web.find_element(By.XPATH,'//*[@id="app"]/div/div[1]/form/div[2]/input').send_keys('交易密码')
element = web.find_element(By.XPATH,'//*[@id="app"]/div/div[1]/form/button/label')
web.execute_script("arguments[0].click();", element)
sleep(2)
cookies=web.get_cookies()
# print(cookies)
cookie=';'.join([f"{i['name']}={i['value']}" for i in cookies])
print(cookie)
with open('cookie.txt', mode='w') as file:
# 写入数据
file.write(cookie)
header={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36',
"Cookie":cookie}
url='https://m.touker.com/trading/baseInfo.json?_=1701498696248'
resp=requests.get(url,headers=header)
print(resp.text)
upload_cookie()
except Exception as e:
print(f"获取cookie失败: {e}")
if datetime.datetime.now().weekday() < 5: # 星期五的 weekday() 返回值是 4
# 每天的9点15分运行函数
schedule.every().day.at("09:15").do(download_cookie)
# 每天的13点45分运行函数
schedule.every().day.at("13:45").do(download_cookie)
while True:
schedule.run_pending()
time.sleep(10)
2024-01-23