英文:
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'])
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论