1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
| from selenium import webdriver 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 import ActionChains import time from PIL import Image import json import random
class CrackSlider(): def __init__(self): super(CrackSlider, self).__init__() self.start_url = 'https://www.bilibili.com/' self.login_url = 'https://passport.bilibili.com/login' self.options = webdriver.ChromeOptions() self.options.add_experimental_option('excludeSwitches', ['enable-automation']) self.options.add_experimental_option('useAutomationExtension', False) self.options.add_argument('--headless')
self.driver = webdriver.Chrome(options=self.options) self.driver.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument', { 'source': 'Object.defineProperty(navigator, "webdriver", {get: () => undefined})' }) self.driver.set_window_size(1366, 768) self.wait = WebDriverWait(self.driver, 10)
def save_cookie(self): self.driver.get(self.login_url) usercode_img = WebDriverWait(self.driver, 20).until( EC.presence_of_element_located( (By.CLASS_NAME, 'qrcode-img') ) ) self.driver.save_screenshot('screenshot.png') left = usercode_img.location['x'] top = usercode_img.location['y'] rigth = usercode_img.location['x'] + usercode_img.size['width'] bottom = usercode_img.location['y'] + usercode_img.size['height']
im = Image.open('screenshot.png') im = im.crop((left, top, rigth, bottom)) im.save('vcode.png') print('请扫码登录') time.sleep(2) img=Image.open('vcode.png') img.show() current_url = self.login_url while current_url: if self.driver.current_url != current_url: current_url = False time.sleep(3) print('登录成功,正在保存cookies') cookies = self.driver.get_cookies() json_cookies = json.dumps(cookies) with open('cookies_blbl.json', 'w') as f: f.write(json_cookies)
def add_cookie(self): self.driver.delete_all_cookies() try: with open('cookies_blbl.json', 'r', encoding='utf-8') as f: list_cookies = json.loads(f.read()) for i in list_cookies: self.driver.add_cookie(i) except: self.save_cookie()
def logo(self): print('开始登录中...') self.driver.get(self.login_url) self.add_cookie() self.driver.get(self.start_url) time.sleep(3) if self.driver.current_url != self.start_url: self.save_cookie() print('使用cookies登录成功')
def look_b(self): sp_list = [] self.driver.get('https://www.bilibili.com/v/popular/rank/all') time.sleep(3) sps = self.driver.find_elements_by_xpath('//ul[@class="rank-list"]/li/div[2]/div[2]/a') for sp in sps: sp_url = sp.get_attribute('href') sp_list.append(sp_url) for i in range(7): sjh = random.randint(0, len(sp_list) - 1) self.driver.get(sp_list[sjh]) print('开始前往{}看视频三连'.format(sp_list[sjh])) time.sleep(random.randint(5,10)) spot = self.driver.find_element_by_id('arc_toolbar_report').find_element_by_class_name('like') ActionChains(self.driver).click_and_hold(spot).perform() time.sleep(5) ActionChains(self.driver).release(spot).perform() time.sleep(random.randint(30, 60))
if __name__ == '__main__': c = CrackSlider() c.logo() c.look_b()
|