下载与推文链接附加的图像的方法是什么?

huangapple go评论62阅读模式
英文:

How to download images attached to a tweet from the tweet link?

问题

如何使用Python从推特链接下载图片,因为Twitter API不再免费?

英文:

Since the Twitter API is no longer free how to download images from tweet links using Python?

答案1

得分: 1

Since the tweet page is dynamically generated, using BeautifulSoup to generate HTML won't work. Hence, I have used Selenium to generate the HTML.

from bs4 import BeautifulSoup as bs
from selenium import webdriver
import time

browser = webdriver.Chrome()
base_url = tweet_url
browser.get(base_url)
time.sleep(2)
html = browser.page_source
soup = bs(html, "html.parser")
images = soup.find_all('img')
for img in images:
    if img['alt'] == 'Image':
        print(img['src'])

(由于文本中有代码部分,我已经跳过了不需要翻译的部分,只翻译了文字内容。)

英文:

Since the tweet page is dynamically generated, using BeautifulSoup to generate html won't work. Hence I have used selenium to generate the html.

from bs4 import BeautifulSoup as bs
from selenium import webdriver
import time

browser = webdriver.Chrome()
base_url = tweet_url
browser.get(base_url)
time.sleep(2)
html = browser.page_source
soup = bs(html, "html.parser")
images = soup.find_all('img')
for img in images:
    if img['alt'] == 'Image':
        print(img['src'])

huangapple
  • 本文由 发表于 2023年5月23日 01:28:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76308628.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定