Skip to content

Instantly share code, notes, and snippets.

@h4k1m0u
Last active May 16, 2021 22:00
Show Gist options
  • Save h4k1m0u/3e2b519efe6d01caef0d21750925e9e7 to your computer and use it in GitHub Desktop.
Save h4k1m0u/3e2b519efe6d01caef0d21750925e9e7 to your computer and use it in GitHub Desktop.
Show on CLI tweets from the profile page of a given USER with Selenium
""" Get tweets from the profile page of a given USER """
import sys
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# get twitter user from command-line
if len(sys.argv) != 2:
print('Usage: python ./script.py USER')
sys.exit(2)
user = sys.argv[1]
# browse to webpage in firefox
driver = webdriver.Firefox()
driver.get('https://twitter.com/{}'.format(user))
try:
# wait for a single article (i.e. tweet) to load async
WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.TAG_NAME, 'article')))
except TimeoutException as err:
print('Error: {}'.format(err))
else:
# get tweets on first page (if no exception thrown)
articles = driver.find_elements_by_tag_name('article')
for article in articles:
spans = article.find_elements_by_tag_name('span')
for span in spans:
# ignore numbers of rt and fav
if span.text.isdigit():
continue
print(span.text, end=' ')
print(end='\n')
finally:
# close opened browser tab
driver.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment