Skip to content

Instantly share code, notes, and snippets.

@rakeshsukla53
Created April 16, 2017 23:21
Show Gist options
  • Save rakeshsukla53/d395a63055a639f8b74f78c328040d18 to your computer and use it in GitHub Desktop.
Save rakeshsukla53/d395a63055a639f8b74f78c328040d18 to your computer and use it in GitHub Desktop.
donald trump tweets
from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.set_window_size(1640, 1640)
driver.maximize_window()
driver.get("https://twitter.com/realDonaldTrump")
SCROLL_PAUSE_TIME = 0.5
# Get scroll height
last_height = driver.execute_script("return document.body.scrollHeight")
while True:
# Scroll down to bottom
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
# Wait to load page
time.sleep(SCROLL_PAUSE_TIME)
# Calculate new scroll height and compare with last scroll height
new_height = driver.execute_script("return document.body.scrollHeight")
if new_height == last_height:
break
last_height = new_height
time.sleep(2)
driver.find_element_by_xpath('//*[contains(text(), "Back to top")]').click()
time.sleep(2)
all_the_tweets = driver.find_elements_by_css_selector('.js-tweet-text-container > p')
for tweet in all_the_tweets:
print(tweet.get_attribute('innerText'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment