Ausgabe
Ich möchte das Herunterladen von MP3-Versionen von YouTube-Videos von dieser Website automatisieren: https://ytmp4converter.com/ mit Selen, aber ich bin auf einige Herausforderungen gestoßen, kann mir jemand helfen?
Hier ist der Code, den ich mir bisher ausgedacht habe, der nicht auf den Download-Button klickt.
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.webdriver.common.keys import Keys
import time
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://ytmp4converter.com/")
#How to access elements on the page
#Id, name, class is the hierachical way of searching for specific element within HTML
search = driver.find_element("name", "url")
#Now what this will do is it will return to as an object that represents that search bar
#that we can actually interact with and mess around
search.send_keys("https://www.youtube.com/watch?v=BQogzYUoQWU&list=RDBQogzYUoQWU&start_radio=1") #This sends input on the search bar e.g "test"
search.send_keys(Keys.RETURN) #This is the computer version of pressing the "Enter" button
#/download.php?source=youtube&media=OA==
#link = driver.find_element(By.LINK_TEXT, "https://ytmp4converter.com/en/wp-content/plugins/aio-video-downloader/download.php?source=youtube&media=OA==")
#link.click()
wait = WebDriverWait(driver, 20)
element = wait.until(EC.presence_of_element_located((By.LINK_TEXT, "https://ytmp4converter.com/en/wp-content/plugins/aio-video-downloader/download.php?source=youtube&media=OA==" )))
element.click()
kann mir jemand zeigen, dass ich falsch liege. Dies ist der Fehler, auf den ich stoße.
Traceback (most recent call last):
File "C:\Users\SAMWEL~1\AppData\Local\Temp\atom_script_tempfiles\a3051e50-4690-11ed-bd2c-11f5bac330f8", line 34, in <module>
element = wait.until(EC.presence_of_element_located((By.LINK_TEXT, "https://ytmp4converter.com/en/wp-content/plugins/aio-video-downloader/
download.php?source=youtube&media=OA==" )))
File "C:\Users\****** ******\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages
\selenium\webdriver\support\wait.py", line 90, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
Stacktrace:
Backtrace:
Ordinal0 [0x008BDF13+2219795]
Ordinal0 [0x00852841+1779777]
Ordinal0 [0x0076423D+803389]
Ordinal0 [0x00793025+995365]
Ordinal0 [0x007931EB+995819]
Ordinal0 [0x007C0F52+1183570]
Ordinal0 [0x007AE844+1108036]
Ordinal0 [0x007BF192+1175954]
Ordinal0 [0x007AE616+1107478]
Ordinal0 [0x00787F89+950153]
Ordinal0 [0x00788F56+954198]
GetHandleVerifier [0x00BB2CB2+3040210]
GetHandleVerifier [0x00BA2BB4+2974420]
GetHandleVerifier [0x00956A0A+565546]
GetHandleVerifier [0x00955680+560544]
Ordinal0 [0x00859A5C+1808988]
Ordinal0 [0x0085E3A8+1827752]
Ordinal0 [0x0085E495+1827989]
Ordinal0 [0x008680A4+1867940]
BaseThreadInitThunk [0x75E7FA29+25]
RtlGetAppContainerNamedObjectPath [0x77E77A9E+286]
RtlGetAppContainerNamedObjectPath [0x77E77A6E+238]
Und hier ist der HTML-Code, der diese Schaltfläche umgibt.
Lösung
Eine langsamere, aber einfachere Lösung mit der youtube_dl-Bibliothek.
Vergiss es nichtpip install youtube_dl
import youtube_dl
video_url = 'https://www.youtube.com/watch?v=BQogzYUoQWU'
video_info = youtube_dl.YoutubeDL().extract_info(
url=video_url, download=False
)
filename = f"{video_info['title']}.mp3"
options = {
'format': 'bestaudio/best',
'keepvideo': False,
'outtmpl': filename,
}
with youtube_dl.YoutubeDL(options) as ydl:
ydl.download([video_info['webpage_url']])
print("Download complete... {}".format(filename))
Beantwortet von – Robert Kadak
Antwort geprüft von – Gilberto Lyons (FixError Admin)