1、在以下代码中输入自己的账号密码:
import time
import requests
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains
def basic():
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.implicitly_wait(30)
driver.maximize_window()
# driver = webdriver.Chrome()
return driver
def get_login(driver, URL, username, password):
driver.get(URL)
driver.find_element_by_class_name("quick_user_avatar").click() # 点击头像
time.sleep(3)
driver.find_element_by_xpath('//div[@class="login_btns"]/a[@class="btn_qq _login_type_item"]').click() # 点击请求登录
time.sleep(3)
# 选择进入内联框架
driver.switch_to.frame("_login_frame_quick_")
driver.switch_to.frame("ptlogin_iframe")
driver.find_element_by_link_text("帐号密码登录").click() # 点击账号密码登录
'''输入账号密码登录'''
input_u = driver.find_element_by_id("u").send_keys(username)
input_p = driver.find_element_by_id("p").send_keys(password)
time.sleep(2)
button = driver.find_element_by_id("login_button").click()
time.sleep(3)
'''通过头像进入到个人中心'''
# 1.定位到要悬停的元素
element = driver.find_element_by_xpath('//*[@id="mod_head_notice_trigger"]/img[1]')
# 2.对定位到的元素执行鼠标悬停操作
ActionChains(driver).move_to_element(element).perform()
time.sleep(3)
# 3.点击昵称进入个人中心
driver.find_element_by_xpath('//*[@id="mod_head_notice_pop"]/div/div[1]/a[1]').click()
# 获取cookie
cookie_items = driver.get_cookies()
cookie_str = ""
# 组装cookie字符串
for item_cookie in cookie_items:
item_str = item_cookie["name"] + "=" + item_cookie["value"] + "; "
cookie_str += item_str
# print(item_cookie)
# 打印出来看一下
# print(cookie_str)
return cookie_str
def start(cookies):
try:
s = requests.session()
this_time = int(round(time.time() * 1000))
login_url = 'https://vip.video.qq.com/fcgi-bin/comm_cgi?name=hierarchical_task_system&cmd=2&_=' + str(this_time)
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.204 Safari/537.36',
'Cookie': cookies
}
res = s.get(login_url, headers=headers).text
print('访问结果:' + res)
except Exception as e:
print(e)
def main():
username = "your_user_name"#帐号
password = "your_password"#密码
URL = "https://film.qq.com/"
driver = basic()
cookies = get_login(driver, URL, username, password)
'''app每天签到'''
start(cookies)
if __name__ == '__main__':
main()