qq cookie
from selenium import webdriver
from selenium.webdriver import ActionChains
import time, re, os, requests
from PIL import Image
def get_cookie():
print('开始')
chrome_options = webdriver.ChromeOptions()
# # 使用headless无界面浏览器模式
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
# 谷歌文档提到需要加上这个属性来规避bug
chrome_options.add_argument('--disable-gpu')
# 设置默认编码为utf-8
chrome_options.add_argument('lang=zh_CN.UTF-8')
## 隐藏滚动条, 应对一些特殊页面
chrome_options.add_argument('--hide-scrollbars')
# 禁止加载图片
# chrome_options.add_argument('blink-settings=imagesEnabled=false')
# 指定浏览器分辨率
chrome_options.add_argument('window-size=1440x900')
driver = webdriver.Chrome(chrome_options=chrome_options, executable_path='/root/yu/01-chromedriver/chromedriver')
driver.get('https://www.wegame.com.cn/store/games')
# driver.maximize_window()
ac = driver.find_element_by_xpath('//*[@id="header_wrapper"]/div/div[3]/div[1]/a/span')
ActionChains(driver).move_to_element(ac).click(ac).perform()
time.sleep(1)
iframe = driver.find_element_by_xpath('/html/body/div[4]/div/div/div[2]/div[3]/iframe') # 切换到这个iframe 不切换获取不到xpath坐标
driver.switch_to.frame(iframe)
time.sleep(2)
ac = driver.find_element_by_xpath('//*[@id="switcher_plogin"]')
ActionChains(driver).move_to_element(ac).click(ac).perform()
time.sleep(1)
driver.find_element_by_xpath('//input[@id="u"]').clear()
for i in '2649942575':
# for i in '1064145110':
driver.find_element_by_xpath('//input[@id="u"]').send_keys(i)
time.sleep(0.2)
driver.find_element_by_xpath('//input[@id="p"]').clear()
for i in 'yu1064145110':
# for i in 'zhangyan1216':
driver.find_element_by_xpath('//input[@id="p"]').send_keys(i)
time.sleep(0.2)
ac = driver.find_element_by_xpath('//*[@id="login_button"]')
ActionChains(driver).move_to_element(ac).click(ac).perform()
time.sleep(2)
print('-' * 100)
html = driver.page_source
if '安全验证' in html:
imgelement = driver.find_element_by_xpath('//*[@id="qrlogin_img"]')
locations = imgelement.location # 获取当前位置坐标 定点
print('locations:{}'.format(locations))
sizes = imgelement.size # 获取xpath取到的图片大小
print('sizes:{}'.format(sizes))
driver.switch_to.default_content() # 切换到主页
iframe_l = driver.find_element_by_xpath('/html/body/div[4]/div/div/div[2]/div[3]/iframe')
locations_l = iframe_l.location
print('locations_l:{}'.format(locations_l))
rangle = (locations['x'] + locations_l['x'], locations['y'] + locations_l['y'],
locations['x'] + sizes['width'] + locations_l['x'],
locations['y'] + sizes['height'] + locations_l['y']) # 要切图片的坐标(x, y, x, y) 第一个是 第一点坐标 后面xy 是第二点坐标 两点获取一个面
path = os.path.dirname(os.path.abspath(__file__))
driver.save_screenshot('{}/iamge.png'.format(path)) # 数据保存为图片
# 打开截图切割
img = Image.open('{}/iamge.png'.format(path)) # 读取要切割的图片
jpg = img.crop(rangle)
jpg.save('{}/iamges.png'.format(path)) # 保存切割下的图片
jpgzoom = Image.open('{}/iamges.png'.format(path))
(x, y) = jpgzoom.size
x_s = x * 3
y_s = y * 3
out = jpgzoom.resize((x_s, y_s), Image.ANTIALIAS)
out.save('{}/zoom.png'.format(path), quality=95) # 保存放大后的图片
# driver.quit()
print('请快速扫码登入')
time.sleep(20)
print('-' * 1000)
# driver.switch_to.default_content()
time.sleep(2)
aa_cookie = driver.get_cookies() # 获取cookie
print("aa_cookie:{}".format(aa_cookie))
print('+' * 100)
cookie = {}
cookies = [] # 拼cookie
for i in aa_cookie:
cookie[i["name"]] = i["value"]
cookies.append('{}={}'.format(i["name"], i["value"]))
print('cookie:{}'.format(cookie))
print('cookies:{}'.format('; '.join(cookies)))
time.sleep(500)
driver.quit()
if __name__ == '__main__':
get_cookie()