Skip to content

Instantly share code, notes, and snippets.

@3t14
Last active July 21, 2022 08:17
Show Gist options
  • Save 3t14/9de9a29b2bcc018f2da0bf45a0029e8e to your computer and use it in GitHub Desktop.
Save 3t14/9de9a29b2bcc018f2da0bf45a0029e8e to your computer and use it in GitHub Desktop.
演習6-18の例
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
browser = webdriver.Chrome()
browser.get('https://www.google.co.jp')
fakebox = browser.find_element(By.XPATH,
'/html/body/div[1]/div[3]/form/div[1]/div[1]/div[1]/div/div[2]/input')
fakebox.send_keys('RPA')
search_button = browser.find_element(By.XPATH,
'/html/body/div[1]/div[3]/form/div[1]/div[1]/div[3]/center/input[1]')
search_button.click()
time.sleep(4)
results = browser.find_elements(By.CLASS_NAME, 'g')
data = []
for result in results:
try:
a = result.find_element(By.TAG_NAME, 'a')
title = result.find_element(By.TAG_NAME,'h3').text
url = a.get_attribute('href')
summary = result.find_element(By.CLASS_NAME,'VwiC3b').text
except Exception as e:
print(e)
continue
data.append([title, url, summary])
f = open('rpa.csv', 'w')
f.write('タイトル, URL, 概要\n')
for row in data:
try:
f.write(','.join(row) + '\n')
except:
pass
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment