Как развернуть окно описания You Tube с помощью кнопки «...подробнее»Python

Программы на Python
Ответить Пред. темаСлед. тема
Anonymous
 Как развернуть окно описания You Tube с помощью кнопки «...подробнее»

Сообщение Anonymous »

Я пытаюсь создать сценарий Selenium для проекта, в котором я хочу установить флажок описания канала Youtube, но, несмотря на все мои усилия, я не могу правильно нажать кнопку «...подробнее» и извлечь временные метки из описания YouTube< /p>
Это мой код

Код: Выделить всё

import sys
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
import re
import time

# Set up Selenium
options = webdriver.ChromeOptions()
options.add_argument('--headless')  # Run in headless mode
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome(options=options)

def extract_timestamps(url, expand_selector="tp-yt-paper-button#expand", timeout=15):
try:
driver.get(url)
time.sleep(5)  # Allow time for dynamic content to load

# Locate the "Show More" button
show_more_button = WebDriverWait(driver, timeout).until(
EC.presence_of_element_located((By.CSS_SELECTOR, expand_selector))
)

# Check if the button is visible and enabled
if not show_more_button.is_displayed() or not show_more_button.is_enabled():
print("Button is not interactable. Trying alternate methods.")
driver.execute_script("arguments[0].scrollIntoView();", show_more_button)
time.sleep(1)  # Stabilize after scrolling

# Perform click using JavaScript if standard click fails
try:
show_more_button.click()
except Exception:
print("Standard click failed. Using JavaScript click.")
driver.execute_script("arguments[0].click();", show_more_button)

print("Successfully clicked 'Show More' button.")

# Locate the expanded description
description_element = WebDriverWait(driver, timeout).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "#description-inline-expander"))
)
description = description_element.text
print("Description:")
print(description)

# Extract timestamps using regex
timestamps = re.findall(r'\d{1,2}:\d{2}', description)
return description, timestamps
except TimeoutException:
print("Timeout while waiting for an element.")
return "", []
except Exception as e:
print(f"Error extracting timestamps: {e}")
return "", []
finally:
driver.quit()

# YouTube URL
youtube_url = "https://youtu.be/iTmlw3vQPSs"
description, timestamp_list = extract_timestamps(youtube_url)

# Output timestamps
if timestamp_list:
print("Extracted Timestamps:")
for timestamp in timestamp_list:
print(timestamp)
else:
print("No timestamps found in the description.")

# Save URL and timestamps to a file
file_name = "youtube_data.txt"
with open(file_name, "w") as file:
file.write(f"URL: {youtube_url}\n")
file.write(f"Timestamps: {', '.join(timestamp_list)}\n")

print(f"\nThe data has also been saved to {file_name}")
Вот результат, который я получаю

Код: Выделить всё

Button is not interactable. Trying alternate methods.
Standard click failed. Using JavaScript click.
Successfully clicked 'Show More' button.
Description:
This is description text having some timestamps.

…
...more
No timestamps found in the description.

The data has also been saved to youtube_data.txt
Я хочу нажать эту кнопку ниже и извлечь данные
[img]https:// i.sstatic.net/YFouNb3x.png[/img]


Подробнее здесь: https://stackoverflow.com/questions/792 ... ore-button
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «Python»