Search code examples
pythonseleniumtwitteryoutubebots

Extract YouTube link from Tweet using Python and selenium


I am trying to phish the YouTube link from a specific tweet and open it in the browser.

I open the URL for the Tweet which is this (https://twitter.com/MrSlagovich/status/1189669505829670912) and then i try to get the YouTube link out of it. Which is this (https://youtu.be/le2v90CQyhs)

I've tried using find element by name find element by tag and I can't seem to fish the url out of the page. Any help would be great. Below is my current code for tying to get it.

YTLink = bot.find_element_by_tag_name("iframe").get_attribute("src")


Solution

  • Give this a try:

    url = 'https://twitter.com/MrSlagovich/status/1189669505829670912'
    
    driver = webdriver.Chrome()
    driver.get(url)
    
    overlay=driver.find_element_by_id("permalink-overlay-dialog")
    YTLink=overlay.find_element_by_xpath("//a[@class='twitter-timeline-link u-hidden']").get_attribute("title")