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>The problem is that you are opening the output file in <code>get_single_item_data</code>, and it is getting closed when that function returns and <code>f</code> goes out of scope.
You want to pass an open file in to <code>get_single_item_data</code> so multiple rows will be written.</p>

python csv