import json
import time
import requests
listHeader = {
"Referer": "https://www.xsj21.com/",
"Host": "www.xsj21.com",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36",
"Cookie": "UM_distinctid=1786d859f0e48c-0e8fd993163825-5771133-1fa400-1786d859f0fa5d; _hjid=24de6ef6-cad5-45fe-b90d-4161453238d7; _uab_collina=161700933152900390583045; p_h5_u=EBD9C439-E500-4161-ABF4-3991E777EDA2; selectedStreamLevel=SD; _hjTLDTest=1; Hm_lvt_ea92613c20acf7b7f7b4aea4253a535f=1616745176,1617009304,1617068049,1617155487; Hm_lpvt_ea92613c20acf7b7f7b4aea4253a535f=1617155487; CNZZDATA1271362856=1864746401-1616745184-%7C1617152235; _hjAbsoluteSessionInProgress=0"
}
from selenium import webdriver
import json
options = webdriver.ChromeOptions()
browser = webdriver.Chrome(options=options)
def init():
browser.get('https://www.xsj21.com/resource')
# browser.add_cookie(c)
# browser.get('https://www.xsj21.com/resource') # 进入b2p4.cn
time.sleep(25)
dictCookies = browser.get_cookies();
jsonCookies = json.dumps(dictCookies) # 转换成字符串保存
with open('damai_cookies.txt', 'w') as f:
f.write(jsonCookies)
print('cookies保存成功!')
def initb():
browser.get('https://www.xsj21.com/resource')
# browser.add_cookie(c)
browser.get('https://www.xsj21.com/resource') # 进入b2p4.cn
return browser
def addcookie(browser):
with open('damai_cookies.txt', 'r', encoding='utf8') as f:
listCookies = json.loads(f.read())
# 往browser里添加cookies
for cookie in listCookies:
cookie_dict = {
'name': cookie.get('name'),
'value': cookie.get('value'),
'domain': cookie.get('domain'),
'httpOnly': cookie.get('httpOnly')
}
# browser.get('https://www.xsj21.com/resource')
browser.add_cookie(cookie_dict)
browser.refresh()
# browser.get('https://www.xsj21.com/resource')
# html = browser.page_source # 获取源代码
# print(html)
# b = initb()
# addcookie(b)
# 模拟浏览器登录
try:
browser.get("https://www.xsj21.com/login")
username = browser.find_elements_by_css_selector("input[data-type='phone']")[0]
password = browser.find_elements_by_css_selector("input[data-type='password']")[0]
log_button = browser.find_elements_by_css_selector(".action-submit")[0]
'''
这一句sleep如果不加,则会提示Message: Element <input id="username" class="user-name" name="username" type="text"> could not be scrolled into view
原因是页面还没加载完成,屏幕上没有发现username这个输入框。
'''
time.sleep(5)
username.clear()
username.send_keys('18322286705')
password.clear()
password.send_keys('8858jxdd')
time.sleep(5)
log_button.click()
yiUp = browser.find_elements_by_css_selector("ul li a[href='/resource']")[0];
yiUp.click()
print("三年级同步微课上册")
time.sleep(8)
yi = browser.find_elements_by_css_selector("label[for='2-1']")[0]
yi.click()
print("一年级")
time.sleep(10)
browser.refresh()
print("刷新...")
time.sleep(30)
print("休眠30s")
html = browser.page_source
print(html)
except Exception as e:
print(e)
finally:
print('OK')
06-26
1036