Python practice - CSDN automatic login and comments

First, open the CSDN landing page: https://passport.csdn.net/login?code=account , let's take account and password login as an example:

Open the url using selenium:

browser = webdriver.Firefox(executable_path=r"C:\geckodriver.exe")
browser.get("https://passport.csdn.net/login?code=account")

Since it is automatic login, we do not need to manually enter the user name and password and click the login button. We can do it all through the program. What we need to do is to analyze the account and password input box, automatically fill in the account and password, and obtain the login button and click it automatically!

Press F12 to check the element:


After the control is obtained, the writer:

browser = webdriver.Firefox(executable_path=r"C:\geckodriver.exe")
browser.get("https://passport.csdn.net/login?code=account")
account = browser.find_element_by_id('all')
pwd = browser.find_element_by_id('password-number')
btn = browser.find_element_by_class_name('form-submit').find_element_by_tag_name('button')
account.send_keys("150xxxxxxxx")
pwd.send_keys("*********")
btn.click()

Note that the account and password are your own. Then execute the program and observe the browser. Under normal circumstances, you will log in directly!

Under abnormal conditions, the verification box will pop up:

At this point, we must simulate sliding through selenium! Before that, we still need F12 to locate the slider element:

Slider keys:

  • Obtain the slider (the above method);
  • Get the required sliding distance:
  • Generate sliding track;
  • When sliding, accelerate first and then decelerate (artificial simulation);

The following method of generating sliding trajectory is derived from the network:

def get_tracks(distance):
    # Moving track
    tracks = []
    # Current displacement
    current = 0
    # Deceleration threshold
    mid = distance * 4 / 5
    # Calculation interval
    t = 0.2
    # Initial velocity
    v = 200
    while current < distance:
        if current < mid:
            # The acceleration is positive 2
            a = 5
        else:
            # The acceleration is negative 3
            a = -3
        # Initial speed v0
        v0 = v
        # Current speed
        v = v0 + a * t
        # Moving distance
        move = v0 * t + 1 / 2 * a * t * t
        # Current displacement
        current += move
        # Add track
        tracks.append(round(move))
    return tracks

To simulate slider sliding, we need ActionChains to simulate human behavior:

try:
    wait = WebDriverWait(driver, 10)
    # Get slider sliding area
    slide = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'div#nc_1__scale_text > span.nc-lang-cnt')))
    # Get slider
    slide_button = wait.until(EC.element_to_be_clickable((By.ID, 'nc_1_n1z')))
    # Gets the sliding distance of the slider
    width = slide.size['width'] - slide_button.size['width']
    # Generate sliding track
    tracks = get_tracks(width)
    # Sliding slider
    action = ActionChains(driver)
    action.click_and_hold(slide_button).perform()
    for distance in tracks:
        action.move_by_offset(xoffset=distance, yoffset=0).perform()
    time.sleep(0.5)
    action.release().perform()
except Exception as e:
    print(str(e))

WebDriverWait is used to wait for an element to be loaded before processing. It is different from time.sleep. Time.sleep can only set a fixed waiting time. The disadvantage is that the setting time is too short but the loading time is too long, or the setting time is too long but the loading time is very short!

Theoretically, this is OK, but in fact, the website has basically done anti climbing processing. The principle is to detect whether you use webdriver. Once it is detected, no matter how much you simulate, it cannot pass the verification, and an error will be prompted:

Therefore, the previous solution on the network was to bypass the detection so that the detection mechanism could not detect the webdriver. There are several methods found on the network:
1:

 driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
     "source": """Object.defineProperty(navigator, 'webdriver', {get: () => undefined})"""
 })

2:

option.add_argument('--disable-blink-features=AutomationControlled')

3:

option.add_argument("start-maximized")
option.add_argument("--disable-blink-features=AutomationControlled")
option.add_experimental_option("excludeSwitches", ["enable-automation"])
option.add_experimental_option("useAutomationExtension", False)

4:

# Wealth.min.js generation method (nodejs needs to be installed): NPX extract wealth evaluations
 with open('../stealth.min.js') as f:
     js = f.read()

 driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
     "source": js
 })

But unfortunately, after two days of various search tests, I can't solve the problem. The only effect is that I don't report errors anymore, but I keep playing the sliding box!

This problem needs to be solved. If there are the latest solutions, please don't hesitate to comment

Continue ~ if the previous login is successful, you will automatically enter the blog details page and automatically fill in the comments and submit:

driver.execute_script("window.location.href='https://baiyuliang.blog.csdn.net/article/details/120473414'")
textarea = driver.find_element_by_id('comment_content')
# send_keys fill in the comments
textarea.send_keys("777")
comment_form = driver.find_element_by_id('commentform')
comment_form.submit()


If you can't get the Comment button directly, because it is invisible before filling in the comment, here I directly get the form of the input box and submit:

Entire py Code:

import time

from selenium import webdriver

from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.chrome.options import Options

option = Options()
option.binary_location = r'D:\Program Files\Chrome\chrome.exe'
driver = webdriver.Chrome(executable_path=r"C:chromedriver.exe", options=option)


def get_tracks(distance):
    # Moving track
    tracks = []
    # Current displacement
    current = 0
    # Deceleration threshold
    mid = distance * 4 / 5
    # Calculation interval
    t = 0.2
    # Initial velocity
    v = 200
    while current < distance:
        if current < mid:
            # The acceleration is positive 2
            a = 5
        else:
            # The acceleration is negative 3
            a = -3
        # Initial speed v0
        v0 = v
        # Current speed
        v = v0 + a * t
        # Moving distance
        move = v0 * t + 1 / 2 * a * t * t
        # Current displacement
        current += move
        # Add track
        tracks.append(round(move))
    return tracks


def move_to_gap(slider, tracks):
    # Analog slider
    action = ActionChains(driver)
    action.click_and_hold(slider).perform()
    # action.reset_actions()   # Clear previous action
    for i in tracks:
        action.move_by_offset(xoffset=i, yoffset=0).perform()
    time.sleep(0.1)
    action.release().perform()


driver.get("https://passport.csdn.net/login?code=account")
account = driver.find_element_by_id('all')
pwd = driver.find_element_by_id('password-number')
btn = driver.find_element_by_class_name('form-submit').find_element_by_tag_name('button')
account.send_keys("Your cell phone number")
pwd.send_keys("Your password")
btn.click()

try:
    wait = WebDriverWait(driver, 10)
    # Get slider sliding area
    slide = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'div#nc_1__scale_text > span.nc-lang-cnt')))
    # Get slider
    slide_button = wait.until(EC.element_to_be_clickable((By.ID, 'nc_1_n1z')))
    # Gets the sliding distance of the slider
    width = slide.size['width'] - slide_button.size['width']
    # Generate sliding track
    tracks = get_tracks(width)
    # Sliding slider
    action = ActionChains(driver)
    action.click_and_hold(slide_button).perform()
    for distance in tracks:
        action.move_by_offset(xoffset=distance, yoffset=0).perform()
    time.sleep(0.5)
    action.release().perform()
except Exception as e:
    print(str(e))

time.sleep(3)  # Wait for 3s

driver.execute_script("window.location.href='https://baiyuliang.blog.csdn.net/article/details/120473414'")
textarea = driver.find_element_by_id('comment_content')
textarea.send_keys("777")
comment_form = driver.find_element_by_id('commentform')
comment_form.submit()

time.sleep(3)  # Wait for 3s

comment_list_box = driver.find_element_by_css_selector('div.comment-list-box')
comment_list = comment_list_box.find_element_by_class_name('comment-list')
comment_line_box = comment_list.find_elements_by_class_name('comment-line-box')
for comment in comment_line_box:
    span_text = comment.find_element_by_class_name('new-comment').text
    print(span_text)

Tags: Python Selenium

Posted on Mon, 27 Sep 2021 03:36:00 -0400 by nutstretch