# 登录爬虫

用来测试登录爬虫的例子:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
import os

def DebugLog(str):
    filepath=os.path.dirname(__file__)+r'/debuglog.txt'
    print("调试文件位置:",filepath)
    if os.path.exists(filepath):
        os.remove(filepath)
    with open(filepath, 'x' , encoding='utf-8') as fp:
        fp.write(str)

cookies=[]
startOptions=webdriver.chrome.options.Options()
# startOptions.headless=True
# startOptions.add_argument("--no-gpu")
# startOptions.add_argument("--no-sandbox")
# browser=webdriver.Chrome(executable_path="/home/jovyan/work/TestScrappy/ArtIntelligenceCompany/chromedriver",chrome_options=startOptions)
browser=webdriver.Chrome()
#browser.set_page_load_timeout(5)
browser.get("https://login.cnki.net/login/?platform=kns&ForceReLogin=1&ReturnURL=https://www.cnki.net/")
browser.implicitly_wait(5)
time.sleep(3)

username = browser.find_element_by_xpath('//input[@id="TextBoxUserName"]')
username.send_keys('username')
password = browser.find_element_by_xpath('//input[@id="TextBoxPwd"]')
password.send_keys('password')
commit = browser.find_element_by_xpath('//div[@class="btn"]/a[@id="Button1"]')
commit.click()
time.sleep(5)

if browser.title == "中国知网":
    login_cookies = browser.get_cookies()
    print("登录成功!")
else:
    print("登录失败!")
pass

searchInput = browser.find_element_by_xpath('//input[@id="txt_SearchText"]')
searchInput.send_keys("人工智能")
browser.find_element_by_xpath('//li[@id="CJFQ"]/i').click()
browser.find_element_by_xpath('//li[@id="CIPD"]/i').click()
browser.find_element_by_xpath('//li[@id="CCND"]/i').click()
browser.find_element_by_xpath('//input[@class="search-btn"]').click()

time.sleep(5)
# WebDriverWait(browser, 10).until(
#         EC.presence_of_element_located((By.CLASS_NAME, "groupsorttitle"))
#     )

#browser.find_element_by_xpath('//span[@class="groupsorttitle"]')
browser.find_element_by_class_name("groupsorttitle");
DebugLog(browser.page_source)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57