iT邦幫忙

2021 iThome 鐵人賽

DAY 5
0
自我挑戰組

使用python撰寫簡單的程式來增進日常生產力系列 第 5

selenium爬蟲:使用xpath

from selenium import webdriver
import openpyxl
import pyautogui
import time

options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
options.add_experimental_option("prefs", {"profile.password_manager_enabled": False, "credentials_enable_service": False})

PATH= "C:/Users/user/Desktop/chromedriver_win32/chromedriver.exe"
driver=webdriver.Chrome(PATH,chrome_options=options)
driver.get('https://www.google.com/')
driver.maximize_window()

workbook=openpyxl.load_workbook(filename='77老大.xlsx')
sheet=workbook['工作表1']

for i in range(1,2):
    cell=sheet.cell(row=i,column=5).value
    print(cell)
    driver.get(cell)
    time.sleep(1)
    
    
    views=driver.find_elements_by_xpath("//*[@id='count']/ytd-video-view-count-renderer/span[1]")
    date=driver.find_elements_by_xpath("//*[@id='info-strings']/yt-formatted-string")
    likes=driver.find_elements_by_class_name("style-scope ytd-toggle-button-renderer.style-text")
    #dislikes=driver.find_elements_by_xpath("//*[@id='text']")
    messages=driver.find_elements_by_class_name("style-scope yt-formatted-string")
    long=driver.find_elements_by_class_name('ytp-time-duration')

    for element in views:
        print('views:')
        print(element.text)
    for element in date:
        print('date:')
        print(element.text)
    for element in likes:
        print('likes:')
        print(element.text)
    for element in messages:
        print('messages:')
        print(element.text)
    for element in long:
        print('long:')
        print(element.text)  
        ```

上一篇
自動化初步-試著用pyautogui操作一般軟體
下一篇
selenium爬蟲:功能擴充
系列文
使用python撰寫簡單的程式來增進日常生產力6

尚未有邦友留言

立即登入留言