简体   繁体   中英

Python get second for loop to second column in excel

So I have a web scraping script from a retail site and I have been scraping names and prices into an excel successfully. I wanted to create more scripts for other products on the same site as well but am having some trouble. Basically in my other scripts I could reach the price by only using 1 container. With this however I had to make another container that is deeper because I don't know how to skip div and span classes. So I had to make 2 for loops as you see and I get the results I want however when exported to excel it exports everything in the first column only. It would help if there was a way to skip classes so that I can only use 1 for loop like previously, or a way to manipulate to data so that I move it to the second column.

from bs4 import BeautifulSoup as soup 
from urllib.request import urlopen as uReq  
import requests
import os.path

page_url = "https://www.tenniswarehouse-europe.com/catpage-MSNIKE-EN.html"

save_path = 'C:\\Users\\Lefty\\Desktop\\Scrape Excels'


response = requests.get(page_url, cookies={'SMID':'smc_fbIdmC7VmLvxzHBD0pXTqbYo7kSYh69hqcKQ02xE_239306669'})
soup = soup(response.text, 'html.parser')



containers = soup.findAll("div", {"class":"product_wrapper shoe cf"})
containers1 = soup.findAll("div", {"class":"pricing"})


filename = "Nikes Men's Shoes"
completeName = os.path.join(save_path, filename+".csv")
f = open(completeName, 'w')

headers = "Name, Price\n"

f.write(headers)

for container in containers:
    name = container.div.img['alt']
    f.write(name + "," + '\n') 

    
for container1 in containers1:
    price = container1.span.span.text
    f.write(price.replace(",", ".") + '\n')

You can use zip function instead of looping for each separeted container. So you would just have one loop to write the file, like this:

for name_container, price_container in zip(containers, containers1):
    name = name_container.div.img['alt']
    price = price_container.span.span.text.replace(",", ".")

    f.write(name+ "," + price + "\n")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM