alias AC='sudo time python3 up_code.py'
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.by import By
import time
import re
import requests
import sys
import os
import json
class UpCodeSpider:
def __init__(self, problem, file):
option = webdriver.ChromeOptions()
option.add_argument('headless')
option.add_argument('no-sandbox')
option.add_argument('disable-dev-shm-usage')
self.browser = webdriver.Chrome('bin/chromedriver',chrome_options=option)
self.username = ''
self.password = ''
self.cookie_file = 'ookies'
self.title = 'null'
self.problem = problem
self.file = file
self.browser.get('http://ox.com/')
with open(self.file, 'r') as file:
self.code = file.read()
with open(self.cookie_file, "r") as f:
cookies = json.load(f)
for cookie in cookies:
self.browser.add_cookie(cookie)
def up_login(self):
print('正在登录' + self.username)
self.browser.get('https://wwcom/login')
try:
self.browser.find_element_by_xpath('/html/body/div[1]/header/nav/div/ul/li[2]/a').click()
self.browser.find_element_by_xpath('//*[@id="login_username"]').send_keys(self.username)
self.browser.find_element_by_xpath('//*[@id="login_password"]').send_keys(self.password)
self.browser.find_element_by_xpath('//*[@id="login-form"]/div[4]/button').click()
except:
pass
self.browser.get('http://.com/')
new_cookies = self.browser.get_cookies()
jsonCookies = json.dumps(new_cookies)
with open(self.cookie_file, 'w') as f:
f.write(jsonCookies)
with open(self.cookie_file, "r") as f:
cookies = json.load(f)
for cookie in cookies:
self.browser.add_cookie(cookie)
def start(self):
self.browser.get('http:///problem/' + str(self.problem))
try:
test = self.browser.find_element_by_link_text('登出')
except Exception as e:
print('cookies失效,重新登录中')
self.up_login()
self.browser.get('http://m/problem/' + str(self.problem))
WebDriverWait(self.browser, 5, 1).until(lambda x: x.find_element_by_link_text('提交'))
self.browser.find_element_by_link_text('提交').click()
print('进入提交')
WebDriverWait(self.browser, 5, 1).until(lambda x: x.find_element_by_xpath('//*[@id="div-answer_answer_editor"]/div[1]'))
self.browser.find_element_by_xpath('//*[@id="div-answer_answer_editor"]/div[1]').click()
self.browser.find_element_by_xpath('//*[@id="div-answer_answer_editor"]/div[1]/div[6]').click()
self.browser.find_element_by_xpath('//*[@id="div-answer_answer_editor"]/div[1]/div[6]/div[1]').click()
self.browser.find_element_by_xpath('//*[@id="div-answer_answer_editor"]/div[1]/div[6]/div[1]/div').click()
self.browser.find_element_by_xpath('//*[@id="div-answer_answer_editor"]/div[1]/div[6]/div[1]/div/div').click()
self.browser.find_element_by_xpath('//*[@id="div-answer_answer_editor"]/div[1]/div[6]/div[1]/div/div/div').click()
self.browser.find_element_by_xpath('//*[@id="div-answer_answer_editor"]/div[1]/div[6]/div[1]/div/div/div/div[5]').click()
self.browser.find_element_by_xpath('//*[@id="div-answer_answer_editor"]/div[1]/div[6]/div[1]/div/div/div/div[5]/div').click()
self.browser.find_element_by_xpath('//*[@id="div-answer_answer_editor"]/div[1]/div[6]/div[1]/div/div/div/div[5]/div/pre').click()
self.browser.find_element_by_xpath('//*[@id="div-answer_answer_editor"]/div[1]/div[1]/textarea').send_keys(self.code)
self.browser.find_element_by_xpath('//*[@id="button-submit-answer"]').click()
print('粘贴代码')
print(self.browser.find_element_by_xpath('/html/body/div/div[3]/div[2]/table/tbody/tr[1]/td[4]/a').text)
try:
while self.browser.find_element_by_xpath('/html/body/div/div[3]/div[2]/table/tbody/tr[1]/td[4]/a').text == 'Waiting':
pass
except Exception as e:
pass
print(self.browser.find_element_by_xpath('/html/body/div/div[3]/div[2]/table/tbody/tr[1]/td[4]/a').text)
spider = UpCodeSpider(sys.argv[1], sys.argv[2])
spider.start()