How to save the extracted data to a file

72 views
Skip to first unread message

APURVA SRIVASTAVA

unread,
Dec 1, 2016, 7:07:28 AM12/1/16
to beautifulsoup
I have extracted the data. Its being printed on the command line but I want to save that to a text file. I tried doing that and the text file gets created but that's empty.

Here is my code, please tell me what to add in my code so that I can save the scraped data to a text file.


import urllib2

from bs4 import BeautifulSoup


page = urllib2.urlopen(url).read()
soup = BeautifulSoup(page, "lxml")
        
        

for tr in soup.find_all('tr')[1:]:
        tds = tr.find_all('th')
        tds = tr.find_all('td')
        print "Nome: %s, Cognome: %s, Email: %s\n" % \
              (tds[0].text, tds[1].text, tds[2].text)        
       

Mark Kapono

unread,
Dec 22, 2016, 11:43:03 AM12/22/16
to beautifulsoup
There is a python module for creating *csv files, which can be opened in Excel or gsheet.
Reply all
Reply to author
Forward
0 new messages