Home>

I'm new to programming. Using selenium with Python 3.6.4,
I want to do web scraping. But in the page
Extract necessary elements specified by xpath and save them in CSV file
I'm going to look at the contents, but the following strings are lined up.
How can you preserve the contents of an element?
I would be happy if you could tell me.

Error message

The contents of the CSV file are as follows.

"[<selenium.webdriver.remote.webelement.WebElement (session =" "0daf7b54557cdd8a62f84f4ef9eca9bd" ", element =" "0.5295562174369803-1" ")&gt ;,<selenium.webdriver.remote.webelement .WebElement (session = "" 0daf7b54557cdd8a62f84f4ef9eca9bd "", element = "" 0.5295562174369803-2 "")&gt ;,<selenium.webdriver.remote.webelement.WebElement (session = "" 0daf7b54557cdd8a62f84f4ef9eca9b0.5 "562 3 "")&gt ;,
Applicable source code
from selenium import webdriver
import csv
import time

CHROME_PATH = "xxxxxxxxxxxx"
TARGET_URL = "https: // xxxxxxxxxxxx /"
driver = webdriver.Chrome (CHROME_PATH)
driver.get (TARGET_URL)
driver.find_element_by_class_name ("searchInput"). send_keys ("xxxxxxxxxxxx") #Enter a specific keyword in the site search window
driver.find_element_by_class_name ("searchButton"). click () #Click the search button
driver.find_element_by_xpath ('// a [contains (text (), "article")]'). click () #Click on the link containing the string "article"
csvw = csv.writer (open ("output.csv", "w", encoding = "utf-8", newline = "\ n"), delimiter = "\ t") # Prepare CSV file
csvlist = [] #Prepare a list to store necessary elements
for i in range (10):
    comp = driver.find_elements_by_xpath ('// div [@ class = "xxxxxxx"]') Specify the location to be extracted from the HTML file with #xpath and store it in the variable
    csvlist.append (comp) #Store extracted elements in a list prepared in advance
csvw.writerow (csvlist) #Write elements stored in list to CSV file