Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# initialize webdriver
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
# navigate to web page
driver.get("https://www.reddit.com/")
# locate search box
search = driver.find_element_by_name("q")
# enter search term
search.send_keys("scraping")
search.send_keys(Keys.RETURN)
try:
# locate search results
search_results = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.CLASS_NAME, "rpBJOHq2PR60pnwJlUyP0")))
# scrape posts headings
posts = search_results.find_elements_by_css_selector("h3._eYtD2XCVieq6emjKBH3m")
for post in posts:
header = post.find_element_by_tag_name("span")
print(header.text)
finally:
# quit browser
driver.quit()
@sashachirico

This comment has been minimized.

Copy link

@sashachirico sashachirico commented Mar 19, 2021

Hi mate, I can't run che code cause says there's a syntax problem on line 28
for post in posts:

Says is the "for" is wrong.. why?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment