Python write of scraping data to csv file
<p>I wrote simple code which scrape data from website but i'm struggling to save all rows to csv file. Finished script save only one row - it's last occurance in loop.</p>

<pre><code>def get_single_item_data(item_url):
    f= csv.writer(open("scrpe.csv", "wb"))  
    f.writerow(["Title", "Company", "Price_netto"]) 

    source_code = requests.get(item_url)
    soup = BeautifulSoup(source_code.content, "html.parser")

for item_name in soup.find_all('div', attrs={"id" :'main-container'}):
    title = item_name.find('h1').text
    prodDesc_class = item_name.find('div', class_='productDesc')
    company = prodDesc_class.find('p').text
    company = company.strip()

    price_netto = item_name.find('div', class_="netto").text
    price_netto = price_netto.strip()


    #print title, company, ,price_netto

    f.writerow([title.encode("utf-8"), company, price_netto, ])
</code></pre>

<p>Important is to save data to concurrent columns</p>

<p>@PadraicCunningham This is my whole script:</p>

<pre><code>import requests
from bs4 import BeautifulSoup
import csv

url_klocki = "http://selgros24.pl/Dla-dzieci/Zabawki/Klocki-pc1121.html"
r = requests.get(url_klocki)
soup = BeautifulSoup(r.content, "html.parser")

def main_spider(max_page):
    page = 1
    while page &lt;= max_page:
        url = "http://selgros24.pl/Dla-dzieci/Zabawki/Klocki-pc1121.html"
        source_code = requests.get(url)
        soup = BeautifulSoup(source_code.content, "html.parser")

        for link in soup.find_all('article', class_='small-product'):
            url = "http://www.selgros24.pl"
            a = link.findAll('a')[0].get('href')
            href = url + a
            #print href

            get_single_item_data(href)

        page +=1

def get_single_item_data(item_url):
    f= csv.writer(open("scrpe.csv", "wb"))   
    f.writerow(["Title", "Comapny", "Price_netto"]) 

    source_code = requests.get(item_url)
    soup = BeautifulSoup(source_code.content, "html.parser")

    for item_name in soup.find_all('div', attrs={"id" :'main-container'}):
        title = item_name.find('h1').text
        prodDesc_class = item_name.find('div', class_='productDesc')
        company = prodDesc_class.find('p').text
        company = company.strip()

        price_netto = item_name.find('div', class_="netto").text
        price_netto = price_netto.strip()


        print title, company, price_netto

        f.writerow([title.encode("utf-8"), company, price_netto])


main_spider(1)
</code></pre>

python csv