Skip to content

Instantly share code, notes, and snippets.

@dimitryzub
Created July 19, 2021 08:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dimitryzub/5f2dc5b63bad02bd1ed1014d49b8e7ce to your computer and use it in GitHub Desktop.
Save dimitryzub/5f2dc5b63bad02bd1ed1014d49b8e7ce to your computer and use it in GitHub Desktop.
from selenium import webdriver
driver = webdriver.Chrome(executable_path='PATH/TO/chromedriver.exe')
# &iax=about - expanded knowledge graph
driver.get('https://duckduckgo.com/?q=elon musk&kl=us-en&ia=web&iax=about')
title = driver.find_element_by_css_selector('.module__title__link').text
try:
website = driver.find_element_by_css_selector('.js-about-item-link').text
except:
website = None
description = driver.find_element_by_css_selector('.js-about-item-abstr').text.strip()
description_link = driver.find_element_by_css_selector('.js-about-item-more-at-inline').get_attribute('href')
thumbnail = driver.find_element_by_css_selector('.module__image img').get_attribute('src')
print(f"{title}\n{website}\n{description}\n{description_link}\n{thumbnail}")
for knowledge_graph_fact in driver.find_elements_by_css_selector('.js-about-module-content .about-info-box__info-row'):
key_element = knowledge_graph_fact.find_element_by_css_selector('.js-about-module-content .about-info-box__info-label').text.replace(':', '').strip()
key_value = knowledge_graph_fact.find_element_by_css_selector('.js-about-module-content .about-info-box__info-value').text.strip()
print(f"{key_element}: {key_value}")
for profile in driver.find_elements_by_css_selector('.js-about-profile-link'):
profile_name = profile.get_attribute('title')
profile_link = profile.get_attribute('href')
profile_thumbnail = f"https://duckduckgo.com{profile.find_element_by_css_selector('.js-about-profile-link .about-profiles__img').get_attribute('src')}"
print(f'\n{profile_name}\n{profile_link}\n{profile_thumbnail}')
driver.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment