I can't get the slicing to work properly. I have a list of strings looking like this:
['subdomain', 'name', 'url']
['https://www.pedidosya.com.ar/restaurantes/buenos-aires/recoleta/empanadas-delivery?bt=RESTAURANT&page=1', 'Cümen-Cümen Empanadas Palermo', 'https://www.pedidosya.com.ar/restaurantes/buenos-aires/cumen-cumen-empanadas-palermo-menu']
['https://www.pedidosya.com.ar/restaurantes/buenos-aires/recoleta/empanadas-delivery?bt=RESTAURANT&page=1', 'Cümen-Cümen Empanadas - Barrio Norte', 'https://www.pedidosya.com.ar/restaurantes/buenos-aires/cumen-cumen-empanadas-barrio-norte-menu']
What I need is to save the 'url' in a new list to further work on it.
This is what I'm trying
for row[3:3] in reader:
menus = []
menus.append[row]
But this is what I get when I print():
['https://www.pedidosya.com.ar/restaurantes/buenos-aires/recoleta/empanadas-delivery?bt=RESTAURANT&page=5', 'La Pergola - Recoleta', 'https://www.pedidosya.com.ar/restaurantes/buenos-aires/la-pergola-recoleta-menu']
Which is the last part of the list. What I need is:
menus = ['https://www.pedidosya.com.ar/restaurantes/buenos-aires/cumen-cumen-empanadas-palermo-menu', 'https://www.pedidosya.com.ar/restaurantes/buenos-aires/cumen-cumen-empanadas-barrio-norte-menu']
I've added the rest of the code. The issue is that it's not a list of str as I thought but type() = '_csv.reader'
Here is the entire code:
urls = ["https://www.pedidosya.com.ar/restaurantes/buenos-aires/recoleta/empanadas-delivery",]
with open("output1.csv", 'w', newline='') as csvfile:
writer = csv.writer(csvfile, delimiter=',')
writer.writerow(['subdomain', 'name', 'url'])
for url in urls:
base = url+ "?bt=RESTAURANT&page="
page = 1
restaurants = []
while True:
soup = bs(requests.get(base + str(page)).text, "html.parser")
sections = soup.find_all("section", attrs={"class": "restaurantData"})
if not sections: break
for section in sections:
for elem in section.find_all("a", href=True, attrs={"class": "arrivalName"}):
restaurants.append({"name": elem.text, "url": elem["href"],})
writer.writerow([base+str(page),elem.text,elem["href"]])
page += 1
#reading
file = open("output1.csv", 'r')
reader = csv.reader(file)