0

I am trying to do web scraping to the amazon deals page to obtain 4 data, (Title, sold by,% of sale, time remaining), but in my variables the results are not separated as a list, this prevents me from exporting this information into a csv / excel.

I'm just learning python and I'm very basic, an apology if my question is very basic

from selenium import webdriver
from lxml import html
from time import sleep

driver = webdriver.Chrome('c:/bin/chromedriver')

for page_nb in range(1, 2):
    driver.get('https://www.amazon.com.mx/gp/goldbox/ref=gbps_ftr_s-5_2c3b_page_' + str(page_nb) + '?gb_f_c2xvdC01=dealStates:AVAILABLE%252CWAITLIST%252CWAITLISTFULL%252CEXPIRED%252CSOLDOUT,dealTypes:LIGHTNING_DEAL,page:' + str(page_nb) + ',sortOrder:BY_SCORE,dealsPerPage:48&pf_rd_p=d8b66f14-9e78-4a85-b04f-327a0b562c3b&pf_rd_s=slot-5&pf_rd_t=701&pf_rd_i=gb_main&pf_rd_m=AVDBXBAVVSXLQ&pf_rd_r=5YBFC04YTSW7FDETY9RQ&ie=UTF8')
    #driver.get('https://www.amazon.com.mx/gp/goldbox/ref=gbps_ftr_s-5_2c3b_dlt_DOTD?gb_f_c2xvdC01=dealStates:AVAILABLE%252CWAITLIST%252CWAITLISTFULL%252CEXPIRED%252CSOLDOUT,sortOrder:BY_SCORE,dealTypes:DEAL_OF_THE_DAY&pf_rd_p=d8b66f14-9e78-4a85-b04f-327a0b562c3b&pf_rd_s=slot-5&pf_rd_t=701&pf_rd_i=gb_main&pf_rd_m=AVDBXBAVVSXLQ&pf_rd_r=7X916BEV4BAEXDRBPGG5&ie=UTF8')
    sleep(1)

    tree = html.fromstring(driver.page_source)

    for product_tree in tree.xpath('//div[contains(@id, "101_dealView_")]'):
        title = product_tree.xpath('//*[@id="dealTitle"]/span/text()')
        vendido = product_tree.xpath('//*[@id="shipSoldInfo"]/text()')
        apartado = product_tree.xpath('//*[@class="a-size-mini a-color-secondary inlineBlock unitLineHeight"]/text()')
        tventa = product_tree.xpath('//*[@role="timer"]/text()')
        
    print(title, vendido, apartado, tventa)

driver.close()
1
  • Your question is broad, can you make it a bit specific? Commented Feb 20, 2021 at 22:53

1 Answer 1

1

You could append to a list. Also just use selenium instead of the whole tree aspect.

lst=[]
for page_nb in range(1, 2):
    driver.get('https://www.amazon.com.mx/gp/goldbox/ref=gbps_ftr_s-5_2c3b_page_' + str(page_nb) + '?gb_f_c2xvdC01=dealStates:AVAILABLE%252CWAITLIST%252CWAITLISTFULL%252CEXPIRED%252CSOLDOUT,dealTypes:LIGHTNING_DEAL,page:' + str(page_nb) + ',sortOrder:BY_SCORE,dealsPerPage:48&pf_rd_p=d8b66f14-9e78-4a85-b04f-327a0b562c3b&pf_rd_s=slot-5&pf_rd_t=701&pf_rd_i=gb_main&pf_rd_m=AVDBXBAVVSXLQ&pf_rd_r=5YBFC04YTSW7FDETY9RQ&ie=UTF8')
    time.sleep(5)
    for product_tree in driver.find_elements_by_xpath('//div[contains(@id, "101_dealView_")]'):
        title = product_tree.find_element_by_xpath('.//a[@id="dealTitle"]/span').text
        vendido = product_tree.find_element_by_xpath('.//span[@id="shipSoldInfo"]').text
        apartado = product_tree.find_element_by_xpath('.//span[@class="a-size-mini a-color-secondary inlineBlock unitLineHeight"]').text
        tventa = product_tree.find_element_by_xpath('.//span[@role="timer"]').text
        #print(title, vendido, apartado, tventa)
        lst.append([title, vendido, apartado, tventa])
        

Outputs

[['TECHVIDA Agitadores, Espumador de Leche, Mini Batidor Eléctrico ...',
  'Vendido por TECHVIDA y enviado por Amazon.',
  '82% apartadas',
  '6:07:47'],
 ['CoWalkers Accesorios para guitarra Accesorios de guitarra Acústica/E...',
  'Vendido por CoWalkers y enviado por Amazon.',
  '60% apartadas',
  '4:42:46'],
 ['CoWalkers Bolsa de Cintura táctica para Exteriores, portaherramienta...',
  'Vendido por CoWalkers y enviado por Amazon.',
  '61% apartadas',
  '5:07:46'],
 ['Lamicall Soporte Tablet Multiángulo',
  'Vendido por Lamicall Direct y enviado por Amazon.',
  '13% apartadas',
  '5:02:46'],
 ['Difusor de Aceites Esenciales - Pesoo Difusor Aromaterapia Difus...',
  'Vendido por Pesoo y enviado por Amazon.',
  '72% apartadas',
  '6:22:46'],
 ['VicTsing Adaptador de HDMI a VGA (1080P), Convertidor de Vídeo par...',
  'Vendido por VicTsingDirect y enviado por Amazon.',
  '40% apartadas',
  '4:57:45'],
 ['Reutilizable Nano Tape, TBONEEY Nano Cinta Adhesiva de Doble Car...',
  'Vendido por TBONEEY y enviado por Amazon.',
  '8% apartadas',
  '6:27:45'],
 ['Bolsa de Aseo de Viaje Colgante Organizador y Baño Dopp Higiene...',
  'Vendido por Lumbor37-MX y enviado por Amazon.',
  '34% apartadas',
  '5:42:45'],
 ['YOMYM Fidget Dodecagon –Juguete para Las Manos con 12 ...',
  'Vendido por DONGXUN y enviado por Amazon.',
  '16% apartadas',
  '4:12:44'],
 ['YOMYM Cabezal de ducha Lluvia de alta presión Cabezal de ducha ...',
  'Vendido por YOMYM y enviado por Amazon.',
  '40% apartadas',
  '5:12:44'],
 ['Cheelom Hormas Ensanchadoras de Zapatos Ajustable de 5 pares...',
  'Vendido por CMGB y enviado por Amazon.',
  '80% apartadas',
  '5:47:43'],
 ['CoWalkers Ejercitador de Mano,Bandas de resistencia para ...',
  'Vendido por CoWalkers y enviado por Amazon.',
  '59% apartadas',
  '5:22:43'],
 ['SEASKY Auriculares para Juegos,Auriculares para Juegos ...',
  'Vendido por GITHREE y enviado por Amazon.',
  '58% apartadas',
  '4:47:43'],
 ['Audífonos Gamer con Micrófono para PS4 Xbox One PC, Diadem...',
  'Vendido por Micolindun Global y enviado por Amazon.',
  '8% apartadas',
  '4:17:42'],
 ['Neewer Ajustable Escritorio Suspensión Soporte de Brazo de...',
  'Vendido por iphotoxx y enviado por Amazon.',
  '30% apartadas',
  '6:47:42'],
 ['VicTsing Mouse Pad con Reposa Muñecas, Alfombrilla de Ratón M...',
  'Vendido por VicTsingDirect y enviado por Amazon.',
  '8% apartadas',
  '5:12:42'],
 ['YOMYM Funda para Moto Cubierta de la Motocicleta 190T Oxford Imp...',
  'Vendido por YOMYM y enviado por Amazon.',
  '50% apartadas',
  '6:42:41'],
 ['SoundPEATS Audifonos Inalámbricos Bluetooth5.0, TrueF...',
  'Vendido por TekTek y enviado por Amazon.',
  '37% apartadas',
  '7:12:41'],
 ['SEASKY Webcam con micrófono,Video Cámara Web108...',
  'Vendido por GITHREE y enviado por Amazon.',
  '40% apartadas',
  '5:57:41'],
 ['ANNA TOSANI Dispensador de Agua Automático, Portátil USB ...',
  'Vendido por ANNA TOSANI y enviado por Amazon.',
  '12% apartadas',
  '6:32:41'],
 ['Neewer Chroma Key Verde Chroma Key Azul Fondo Plegable Fondo P...',
  'Vendido por Photo Guard y enviado por Amazon.',
  '26% apartadas',
  '7:02:41'],
 ['Jasonwell Base de construcción Juego de Construcción de Base ...',
  'Vendido por Jasonwell International y enviado por Amazon.',
  '60% apartadas',
  '5:37:41'],
 ['Cheelom Faucet Filtro de agua, Grifo de Filtro purificador de agua...',
  'Vendido por CMGB y enviado por Amazon.',
  '25% apartadas',
  '4:17:41'],
 ['Set de Manicure para Bebé, para cuidado e higiene de las uñas del ...',
  'Vendido por Beetbul y enviado por Amazon.',
  '10% apartadas',
  '6:17:40']]

And so forth

with open("Test.csv", "w", newline="") as f:
   writer = csv.writer(f)
   writer.writerows(lst)
Sign up to request clarification or add additional context in comments.

1 Comment

excellent answer, it worked perfectly for me, it's good to learn this, only now I have another topic, when exporting the lists in a csv both using the csv library or the pandas library it only exports the first data of each list, such as could you modify my code to export everything? import csv header = ['Titulo', 'Sold_by', 'Sold_Perc', 'Time_left'] data = [title, vendido, apartado, tventa] with open('Test.csv', 'w', encoding='UTF8', newline='') as f: writer = csv.writer(f) writer.writerow(header) writer.writerow(data)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.