1 import time 2 from selenium.webdriver.chrome.options import Options 3 from selenium.webdriver import Chrome, ActionChains 4 import requests 5 opt = Options() 6 # 无头浏览器 7 # opt.add_argument('--headless') 8 # opt.add_argument('--disable-gpu') 9 # 屏蔽谷歌浏览器正在接收自动化软件控制提示 10 # opt.add_experimental_option('useAutomationExtension', False) 11 opt.add_experimental_option('excludeSwitches', ['enable-automation']) 12 opt.add_argument('--disable-blink-featurse=AutomationControlled') 13 # 不自动关闭浏览器 14 opt.add_experimental_option("detach", True) 15 web = Chrome(executable_path=r'D:\bigdata\pycharmxiangmu\venv\Scripts/chromedriver.exe',options=opt) 16 web.get('https://www.douyin.com/user/MS4wLjABAAAAJUwAJJ0vxcAx1-uUtcTG6yTIndqqLZJOU5Xvnej5sbD2GdYv818HElibMnCUajp_') 17 script = 'Object.defineProperty(navigator, "webdriver", {get: () => false,});' 18 web.execute_script(script) 19 time.sleep(2) 20 web.maximize_window() 21 web.implicitly_wait(10) 22 n = 1 23 shipindizhi = './抖音个人主页视频/' 24 try: 25 web.find_element_by_xpath('//*[@id="login-pannel"]/div[2]').click() 26 except: 27 print('没有登陆提示') 28 # 下滑到底部 全部数据加载出来 29 for i in range(1000,20000,1000): 30 web.execute_script(f'window.scrollTo(0,{i})') 31 time.sleep(2) 32 href_list = [] 33 for page in range(1, 101): 34 time.sleep(1) 35 href = web.find_element_by_xpath(f'//li[{page}]/a').get_attribute('href') 36 href_list.append(href) 37 # print(len(href_list)) 38 for hrefs in href_list: 39 # 循环请求报错 40 web.get(hrefs) 41 web.implicitly_wait(5) 42 web.find_element_by_xpath('/html/body/div[2]/div/div/div[2]/div/button[text()="取消"]').click() 43 web.implicitly_wait(5) 44 try: 45 web.find_element_by_xpath('//*[@id="login-pannel"]/div[2]').click() 46 except: 47 print('没有扫码登陆提示') 48 time.sleep(2) 49 web.maximize_window() 50 web.implicitly_wait(10) 51 time.sleep(1) 52 # 因为每次绝对地址都会动 所以换成//video/source[1]点一个get获取href属性 53 try: 54 splj = web.find_element_by_xpath('//video/source[1]').get_attribute('src') 55 except: 56 print('图文信息 没有视频') 57 # print(splj) 58 pinjie = shipindizhi + 'shiping_%s' % n + '.mp4' 59 f = open(pinjie, mode='wb') 60 f.write(requests.get(url=splj).content) 61 n += 1 62 print(pinjie + '图片保存成功')