代码拉取完成,页面将自动刷新
同步操作将从 zhangk/京东自动购物 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
import pickle
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By as seleniumBY
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as seleniumEC
from info import Info
class Kdriver:
def __init__(self):
driver = webdriver.Chrome(executable_path=Info.driver_path)
driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
'source': '''
Object.defineProperty(navigator, 'webdriver', {
get: () => undefined
})
'''
})
self.driver = driver
# def wait_element_load(self, element, expiration_time=10, **keywargs):
# while not self.is_exist(element, **keywargs):
# expiration_time -= 2
# if expiration_time < 0:
# return False
# sleep(2)
# return True
def wait_element_load(self, element, expiration_time=10, **kwargs):
try:
WebDriverWait(self.driver, expiration_time)\
.until(seleniumEC.presence_of_element_located((seleniumBY.CSS_SELECTOR,element)))
except TimeoutError:
return False
if len(kwargs) > 0:
return self.is_exist(element, **kwargs)
return True
def is_exist(self, element, **keywargs):
flag = True
try:
target_element = self.driver.find_element_by_css_selector(element)
if len(keywargs) > 0:
for k,v in keywargs.items():
if getattr(target_element,k) != v:
raise NoSuchElementException
return flag
except NoSuchElementException:
flag = False
return flag
def load_cookie(self, cookie):
self.driver.delete_all_cookies()
with open(Info.cookie_path, 'rb') as ck:
cookies = pickle.load(ck)
for cookie in cookies:
self.driver.add_cookie(cookie)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。