[Python selenium] After scraping Google search results, output title and URL in csv

environment

macOS Catalina 10.15.3 Python 3.6.5

Overview

Search Google with any word and get a list of the search results up to any number of pages Output title and url to csv

Method (copy OK)

# !python3
#Get the title and URL of google search result and output csv

import time, chromedriver_binary, os, csv
from selenium import webdriver

output_path = "/Final csv output directory
os.chdir(putput_path)                        

driver = webdriver.Chrome()                 #Prepare chrome

#open html
driver.get("https://www.google.com/")       #Open google
search = driver.find_element_by_name("q")   #Search box"q"To specify
search.send_keys(“xxx yyy zzz“)  #Send search word
search.submit()                             #Perform a search
time.sleep(3)                               #Wait 3 seconds

def ranking(driver):
    i = 1 #Fixed at 1
    i_max = 10 #How many pages do you want to search?
    title_list = []
    link_list = []

    #Loop until the current page exceeds the specified maximum analysis page(i_max)
    while i <= i_max:
        #Title and link are class="r"Is in
        class_group = driver.find_elements_by_class_name("r")
        # class="r"Extract titles and links from,For loop to add to list
        for elem in class_group:
            title_list.append(elem.find_element_by_class_name('LC20lb').text)           #title(class="LC20lb")
            link_list.append(elem.find_element_by_tag_name('a').get_attribute('href'))  #Link(href of a tag)

        #There is only one "Next" button, but I dare to search multiple by elements.An empty list means the last page.
        if driver.find_elements_by_id("pnnext") == []:
            i = i_max + 1   #Without the next page,Forcibly exceeds the maximum number of pages and ends the loop
        else:
            #The URL of the next page is id="pnnext"Href attribute of
            next_page = driver.find_element_by_id("pnnext").get_attribute("href")
            driver.get(next_page)
            i = i + 1       #Go through the page
            time.sleep(3)   #3 seconds break,Repeat this up to the specified maximum number of pages
    return title_list, link_list 

#Execute the ranking function defined above to get the title and URL list
title, link = ranking(driver)

#To spit out with csv[[a,1],[b,2]]Create a list like
result = [list(row) for row in zip(title, link)]

#csv output using result
with open("result.csv", mode="w", encoding="utf-8") as f:
    writer = csv.writer(f, lineterminator="\n")
    writer.writerows(result)

#Close browser
driver.quit()

Recommended Posts

[Python selenium] After scraping Google search results, output title and URL in csv
Scraping Google News search results in Python (2) Use Beautiful Soup
Csv output from Google search with [Python]! 【Easy】
Recursively search for files and directories in Python and output
Scraping with selenium in Python
Scraping with Selenium in Python
[Python scraping] I tried google search top10 using Beautifulsoup & selenium
Scraping with Selenium in Python (Basic)
Scraping with Python, Selenium and Chromedriver
Export and output files in Python
Selenium and python to open google
[Python scraping] Output the URL and title of the site containing a specific keyword to a text file
Data input / output in Python (CSV, JSON)
[Python] Explore the characteristics of the titles of the top sites in Google search results
Practice web scraping with Python and Selenium
[Python] How to name table data and output it in csv (to_csv method)
I tried web scraping using python and selenium
Try running Google Chrome with Python and Selenium
Speed evaluation of CSV file output in Python
Csv in python
[Mac] A super-easy way to execute system commands in Python and output the results
Full-width and half-width processing of CSV data in Python
Implement Depth-First Search (DFS) and Breadth-First Search (BFS) in python
Binary search in Python
Scraping google search (image)
python input and output
Linear search in Python
Japanese output in Python
Scraping with Selenium [Python]
Python web scraping selenium
Binary search in Python (binary search)
I tried [scraping] fashion images and text sentences in Python.
Application to display and search local memos (diary) in Python
Sort and output the elements in the list as elements and multiples in Python.
[Selenium] Change log output destination when executing phantomjs in python3
Google search for the last line of the file in Python
Output product information to csv using Rakuten product search API [Python]