Python Forum
Writing to right spot in CSV and I'm a noob
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Writing to right spot in CSV and I'm a noob
#1
DISCLAIMER: I'm new to python and programming in general(only been coding for a couple days.go.easy.on.me)


I'm trying to write a code that will scrape my website and retrieve [Product Name, Product Price] and write it to a csv file.
when I write it to the csv file the information doesn't go where I want it to go:
[Image: CSVMESSUP_zpsq3vxqisz.png]

I'm not getting any errors thank god Dance

I was wondering if someone would be so kind as to review my code and tell me how to make it work the way I'd like.

#source variables
source = requests.get('https://www.shopshiptrack.com/index.php?route=product/catalog&limit=100').text

soup = BeautifulSoup(source, 'lxml')

#lists
product_lst = []
price_lst = []


#csv creation
with open('list.csv', 'w', newline="") as csv_file:
    csv_writer = writer(csv_file)
    headers = ['Name', 'Price']
    csv_writer.writerow(headers)
# code
    for products_section in soup.find_all('div', class_='name'):
        product_names = products_section.a.text
        product_lst.append(product_names)
    csv_writer.writerow(product_lst)


    for products_price in soup.find_all('span', class_='price-normal'):
        specific_price = products_price.text
        price_lst.append(specific_price)
    csv_writer.writerow(price_lst)
Reply
#2
better use tab delimited csv, there are commas in the table

from bs4 import BeautifulSoup as bsoup
import requests

#source variables
source = requests.get('https://www.shopshiptrack.com/index.php?route=product/catalog&limit=100').text
 
soup = bsoup(source, 'lxml')
 
#lists
product_lst = []
price_lst = []
mylist = []
 
#csv creation
with open('list.csv', 'w', newline="") as csv_file:
    csv_file.write('Name' +  '\t' + 'Price' + '\n')
# code
    for products_section in soup.find_all('div', class_='name'):
        product_names = products_section.a.text
        product_lst.append(product_names) 
 
    for products_price in soup.find_all('span', class_='price-normal'):
        specific_price = products_price.text
        price_lst.append(specific_price)

    for x in range(len(product_lst)):
        mylist.append(product_lst[x] + '\t' + price_lst[x] + '\n')

    for a in range(len(mylist)):
        csv_file.write(mylist[a])
Reply
#3
I fiddled with this forever. The code you gave me put the price & the product together in the same column.
Here's what worked for me:
source = requests.get('https://www.shopshiptrack.com/index.php?route=product/catalog&limit=100').text
soup = BeautifulSoup(source, 'lxml')


# csv creation
with open('list.csv', 'w', newline="") as csv_file:
    csv_writer = writer(csv_file)
    headers = ['Name', 'Price']
    csv_writer.writerow(headers)

    #list variables
    product_list = []
    price_list =[]
    products_prices = []

    #for loops
    for each_name in soup.find_all('div', class_='name'):
        products = each_name.a.text
        product_list.append(products)

    for each_price in soup.find_all('span', class_='price-normal'):
        price = each_price.text
        price_list.append(price)


    final_list = list(zip(product_list, price_list))

    csv_writer.writerows(final_list)
[Image: csvworking_zpslfvo24qa.png]
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020