YouTubeから流行とか把握するために昔作ったけど今はAPIでいいよね
供養
あとYouTubeをスクレイピングしちゃあかんよ
from selenium import webdriver
import time
from selenium.webdriver.common.action_chains import ActionChains
import urllib.parse
def main():
# 検索ワード
search_words = ['α波', '睡眠']
# chromeを開く
driver = webdriver.Chrome('../chromedriver')
s = '+'.join(map(urllib.parse.quote, search_words))
driver.get("https://www.youtube.com/results?search_query=" + s + '&sp=CAM%253D')
info_list = []
time.sleep(1)
for i in range(10):
driver.execute_script("scrollBy(0, 1000);")
for i in range(35, 45):
info = {'title': '', 'url': '', 'channel': '', 'registrant': 0, 'release': ''}
loop_flag = 0
selector = f'#contents > ytd-item-section-renderer:nth-child({i // 20 + 1}) > #contents > ytd-video-renderer:nth-child({20 if i % 20 == 0 else i % 20}) > #dismissable > #video-title > yt-formatted-string'
while loop_flag <= 2:
try:
element = driver.find_element_by_css_selector(selector)
actions = ActionChains(driver)
actions.move_to_element(element)
actions.perform()
info['url'] = element.get_attribute('href')
break
except Exception as e:
print(i, e)
print(selector)
loop_flag += 1
time.sleep(1)
if not info['url'] == '':
info_list += info
print(info_list)
print(len(info_list))
driver.quit()
if __name__ == "__main__":
main()