automation by Pytyon

【python×selenium】getting article’s title from search result of google

Hello, I am Kota.

I’m a  Japanese university student, grade three.

I’m not good at English. So, if you find mistakes, please let me know.

 

Today, I will introduce program for getting article’s title from search result of google.

Overview of the program

・Getting article’s title on first page

・Using python3.7.3 and selenium

 

Source Code

from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys

#setting search words
keywords = ['python','automate']

#ready to search words
searchSentence = ''
for keyword in(keywords):
	searchSentence += (keyword + ' ')

#add return key for search faster
searchSentence += '\n'

waitTime = 2

driver = webdriver.Chrome()
url = "https://google.com"
driver.get(url)
time.sleep(waitTime)

inputBoxElement = driver.find_element_by_xpath('//*[@id="tsf"]/div/div/div/div/div/input')
inputBoxElement.send_keys(searchSentence)
time.sleep(waitTime)

titleElements = driver.find_elements_by_class_name('LC20lb')

#element change to text
#list can't change to text, so change one each
titles = []
for titleElement in(titleElements):
	titles.append(titleElement.text)

for title in(titles):
	print(title)

#finish selenium
driver.quit()

 

Result

$ python3 get_title_from_google.py

Automate the Boring Stuff with Python

Automate the Boring Stuff with Python完走しました – paloma blog

Amazon | Automate the Boring Stuff with Python: Practical … – アマゾン

automate the boring stuff with python automate the … – Studentportalen

Automate the Boring Stuff with Python – gotohayato

PythonでUI Automation – Qiita

Automate the Boring Stuff with Python Programming | Udemy

How to learn to automate things using Python – Quora

Using Python to Automate Tedious Tasks | Our Success Stories …

 

furthermore

This program is not thought about exception.

So, next, I should add exception handling.

 

This program is just collect only title of first page.

I want to add function such as collecting other information, saving on Excel and so on.

thank you.