My code is:
import openpyxl
from selenium import webdriver
from lxml import html
from webdriver_manager.chrome import ChromeDriverManager
from openpyxl import Workbook, load_workbook
import pandas as pd
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("https://www.amazon.com.br/s?k=iphone")
tree = html.fromstring(driver.page_source)
for product_tree in tree.xpath('//div[contains(@data-cel-widget, "search_result_")]'):
title = product_tree.xpath('.//span[@]/text()')
price = product_tree.xpath('.//span[@]/text()')
itens_list = []
itens_iph = {
'title': title,
'price': price,
}
itens_list.append(itens_iph)
print(itens_list)
When im trying to print itens_list, its returning []. I cant find a answer to this problem, and im stuck, could someone please help me? thanks!
CodePudding user response:
Creating a variable inside of a block like a loop or an if statement will mean the variable's scope is that block of code. You need the scope of the variable to be outside of the loop and available to the whole file to do things with it both in and outside of the loop. It also has to be declared before you can append anything so it must be created outside of and before the loop.
CodePudding user response:
You are assigning an empty dictionary inside loop. So after each iteration the itens_list will be empty.
Move iten_list above for loop, It will work.
Here is the correction
import openpyxl
from selenium import webdriver
from lxml import html
from webdriver_manager.chrome import ChromeDriverManager
from openpyxl import Workbook, load_workbook
import pandas as pd
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("https://www.amazon.com.br/s?k=iphone")
tree = html.fromstring(driver.page_source)
itens_list = []
for product_tree in tree.xpath('//div[contains(@data-cel-widget, "search_result_")]'):
title = product_tree.xpath('.//span[@]/text()')
price = product_tree.xpath('.//span[@]/text()')
itens_iph = {
'title': title,
'price': price,
}
itens_list.append(itens_iph)
print(itens_list)
