获取vip cookies抓取vip视频——模拟登录

目的:获取vip cookies,使用该cookies抓取付费视频

工具:vip账号,selenium,chromedriver,mysql

原理:使用vip账号模拟登陆,获取cookies,使用该cookies即可抓取付费视频

代码:

# utf-8

from selenium import webdriver
import pymysql
import json
from selenium.webdriver.chrome.options import Options

db = pymysql.connect(host="",
                     user="",password="",database="",
                     port=3306)
cursor = db.cursor()

# 写入工具路径
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')

driver_path = r"E:\Tools\cd\chromedriver.exe"
driver = webdriver.Chrome(executable_path=driver_path,chrome_options=chrome_options)

url = "https://www.xxx.com"

def get_login():
    driver.get(url)
    # 定位需点击的元素位置
    # print(len(driver.page_source))
    # print(driver.page_source)
    click_btn = driver.find_element_by_class_name("a-avatar_17DKW")
    click_btn.click()
    driver.switch_to.frame('alibaba-login-box')
    user_input = driver.find_element_by_id("fm-login-id")
    user_input.send_keys("")
    pwd_input = driver.find_element_by_id("fm-login-password")
    pwd_input.send_keys("")
    login_btn = driver.find_element_by_class_name("fm-button" and "fm-submit" and "password-login")
    login_btn.click()

    dict = {}
    cookies = driver.get_cookies()
    for cookie in cookies:
        # print(cookie)
        key = cookie['name']
        value = cookie['value']
        dict[key] = value
    # print(dict)
    c_j = json.dumps(dict)
    print(c_j)
    sql = """
    insert into cookies(id,cookies) value(null,%s)
    """
    cursor.execute(sql, (c_j))
    db.commit()

if __name__ == '__main__':
    get_login()

这里只做了cookies的抓取

 

 出现小的登陆页面,然后,此时直接定位账号和密码的输入框,虽然你能找到

但是chromedriver+selenium是不行的。

你必须把整个弹出来的框先定位到iframe框:

driver.switch_to.frame('alibaba-login-box')

 才能定位到两个输入框。

不信?你试试呗。

 

你可能感兴趣的:(爬虫实战)