Python 3 CSV not writing -
when open csv file see nothing. right way build csv file? trying learn all. help.
import csv urllib.request import urlopen bs4 import beautifulsoup html = urlopen("http://shop.nordstrom.com/c/designer-handbags?dept=8000001&origin=topnav#category=b60133547&type=category&color=&price=&brand=&stores=&instoreavailability=false&lastfilter=&sizefinderid=0&resultsmode=&segmentid=0&page=1&partial=1&pagesize=100&contextualsortcategoryid=0") nordhandbags = beautifulsoup(html) baglist = nordhandbags.findall("a", {"class":"title"}) f = csv.writer(open("./nordstrom.csv", "w")) f.writerow(["product title"]) title in baglist: producttitles = title.contents[0] f.writerow([producttitles])
really hard see how fail have @ least "product title"
header in file. checking file after have tgerminated python interpreter? this, because there no explicit close of file in code, , until closed, contents may cached in memory.
more pythonic, , avoiding problem, is
open("./nordstrom.csv", "w") csvfile: f = csv.writer( csvfile) f.writerow(["product title"]) # etc. pass # close block, csvfile closed.
also (grasping @ straws) opening file text editor check it, or using type
command in windows cmd.exe? because, if file doesn't contain explicit lf, c:\wherever\ >
prompt may overwrite header before see it.
Comments
Post a Comment