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
#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


Messages In This Thread
RE: Writing to right spot in CSV and I'm a noob - by faizon - Feb-16-2019, 09:06 AM

Forum Jump:

User Panel Messages

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