我正在学习硒。我写了一些python代码来从网站上抓取数据。它可以工作,但只取出第一个重复的链接并生成csv。因为代码会重复第一个链接中的信息,所以行不通吗?
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
import csv
# Specifying incognito mode as you launch your browser[OPTIONAL]
option = webdriver.ChromeOptions()
option.add_argument("--incognito")
lista_datos = []
resultado = []
# Create new Instance of Chrome in incognito mode
browser = webdriver.Chrome(executable_path=r'C:\Users\inspiron3420\Downloads\chromedriver.exe', chrome_options=option)
# Go to desired website
browser.get("https://www.biobiochile.cl")
# Wait 20 seconds for page to load
timeout = 20
try:
variable = 'incendio'
boton = browser.find_element_by_xpath("//*[@id='search-anchor']")
boton.click()
buscar = browser.find_element_by_xpath("//*[@id='buscador-bbcl']/div/input")
buscar.send_keys(variable)
accion = browser.find_element_by_xpath("//*[@id='buscador-bbcl']/div/span[2]/button")
accion.click()
except TimeoutException:
print("Timed out waiting for page to load")
browser.quit()
try:
WebDriverWait(browser, 5).until(EC.presence_of_all_elements_located((By.CLASS_NAME, "resultados-texto")))
except:
print ("Elementos no encontrados")
#Obtenemos en una lista los elementos encontrados
resultados = browser.find_elements_by_class_name("resultados-texto")
for resultado in resultados:
titulopre = resultado.find_element_by_xpath("//*[@id='menu-buscador2']/div/div[3]/div/div[3]/div[1]/a/div[2]/div[1]")
titulo = titulopre.text
fechapre = resultado.find_element_by_xpath("//*[@id='menu-buscador2']/div/div[3]/div/div[3]/div[1]/a/div[2]/div[2]")
fecha = fechapre.text
#Finalmente metemos en una lista de listas los datos obtenidos
lista_datos.append([titulo,fecha])
csvsalida = open('scrappingbiobio.csv', 'w', newline='')
salida = csv.writer(csvsalida)
salida.writerow(['titulo', 'fecha'])
salida.writerows(lista_datos)
csvsalida.close()
答案 0 :(得分:1)
csvsalida = open('scrappingbiobio.csv', 'w', newline='')
有w
,每次都覆盖
您应该使用append
csvsalida = open('scrappingbiobio.csv', 'a', newline='')
现在的问题是它将每次都写入此行salida.writerow(['titulo', 'fecha'])
。为此,您可以检查文件是否存在
import os
csvsalida = open('scrappingbiobio.csv', 'a', newline='')
salida = csv.writer(csvsalida)
if os.path.isfile('scrappingbiobio.csv') == False:
salida.writerow(['titulo', 'fecha'])
salida.writerows(lista_datos)
csvsalida.close()