how to get link of an image from twitter if the image is isnide a link

215 Views Asked by At

I am trying to get the image links from Twitter posts. When I tried to do this with the Twitter API, I only get the link of images if they are posted as images but most newspapers post a link to their whole report including an image. I am trying to get the image inside that link. This is normally the basic way to get the link example. However, in my case, an image can be hidden under a link like this example from twitter. I have tried solutions like beautifulsoup but the HTML response I get doesn't make any sense. It doesn't include any of the attributes or classes I am searching for. This is the code I tried:

    url = "https://twitter.com/derspiegel/status/1511961740694614016"
    res = requests.get(url)
    
    soup = BeautifulSoup(res.content, "html.parser")
    print(soup)
    comicElem = soup.find_all(class_="css-9pa8cd")
    print(comicElem)

Does anyone have an idea to get this done with Twitter API or any other method?

1

There are 1 best solutions below

0
On

Use selenium that's example cope tutorials website :

from selenium import webdriver
#set chromedriver.exe path
driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")
driver.implicitly_wait(0.5)
#maximize browser
driver.maximize_window()
#launch URL
driver.get("https://www.tutorialspoint.com/index.htm");
#open file in write and binary mode
with open('Logo.png', 'wb') as file:
#identify image to be captured
   l = driver.find_element_by_xpath('//*[@alt="Tutorialspoint"]')
#write file
   file.write(l.screenshot_as_png)
#close browser
driver.quit()

Copying image by id Link to tutorial