The following code applies to the header login to get cookies, 99% pass rate. Hopefully toutiao will provide an API for authorizing articles.

Test address: sso.toutiao.com/login/?serv…

Draw lessons from the text of the link zhuanlan.zhihu.com/p/57675338 zhuanlan.zhihu.com/p/57976223

Server using Cv2 error see here, juejin.cn/post/684490…

The first step is to calculate the gap distance

First send request to the background and notch map, save as a local file, and then use the function FindPic to calculate the notch distance

def FindPic(target, template):
    """Find the best match in the image :param target: the background image :param template: the image to be found :return: return the best match and the worst match and the corresponding coordinates."""
    target_rgb = cv2.imread(target)
    target_gray = cv2.cvtColor(target_rgb, cv2.COLOR_BGR2GRAY)
    template_rgb = cv2.imread(template, 0)
    res = cv2.matchTemplate(target_gray, template_rgb, cv2.TM_CCOEFF_NORMED)
    value = cv2.minMaxLoc(res)
    return value[2:][0][0], value[2:][1][0]  # use value [2:] [1] [0]
Copy the code

The second step is to generate a sliding trajectory based on acceleration and deceleration

Def generate_tracks(distance): distance += 20 v = 0 t = 0.2 forward_tracks = 0 mid = distance * 3/5# Deceleration threshold
    while current < distance:
        if current < mid:
            a = 2  The acceleration is +2
        else:
            a = -3  Acceleration minus 3S = v * t + 0.5 * a * (t ** 2) v = v + a * t += s forward_tracks. -2, -2, -2, -2, -1, -1, -1]return forward_tracks, back_tracks
Copy the code

Third, use Selenium to implement sliding

value_1, value_2 = findpic(target_name, template_name)
action = ActionChains(driver)  Instantiate an action object
button = driver.find_element_by_xpath('//*[@id="validate-drag-wrapper"]/div[2]/img')  # Find the slider
try:
    action.click_and_hold(button).perform()  # perform() is used to perform the actions stored in ActionChains
except StaleElementReferenceException as e:
    pass
action.reset_actions()
forward_tracks, back_tracks = generate_tracks(value_2)
for x in forward_tracks:
    action.move_by_offset(x, 0)  Move the slider forward
for x in back_tracks:
    action.move_by_offset(x, 0)  Move the slider back
action.release().perform()  # Release the mouse
Copy the code