Python3 CSV未写入

Python3 CSV未写入,python,csv,python-3.x,beautifulsoup,Python,Csv,Python 3.x,Beautifulsoup,当我打开我的csv文件时,我什么也看不到。这是构建csv文件的正确方法吗?只是试着去了解这一切。谢谢你的帮助 import csv from urllib.request import urlopen from bs4 import BeautifulSoup html = urlopen("http://shop.nordstrom.com/c/designer-handbags?dept=8000001&origin=topnav#category=

当我打开我的csv文件时,我什么也看不到。这是构建csv文件的正确方法吗?只是试着去了解这一切。谢谢你的帮助

    import csv
    from urllib.request import urlopen
    from 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"])

    for title in bagList:
        productTitles = title.contents[0]
        f.writerow([productTitles])

真的很难看出您怎么会在该文件中没有至少一个
“Product Title”
头。您是否在终止Python解释器后检查该文件?这是因为代码中没有明确关闭文件,并且在关闭文件之前,其内容可能会缓存在内存中

更多的蟒蛇,避免这个问题,是

 with open("./nordstrom.csv", "w") as csvfile:
    f = csv.writer( csvfile)
    f.writerow(["Product Title"])
    # etc.

 pass # close the with block, csvfile is now closed.

另外(抓住救命稻草)您是使用文本编辑器打开文件进行检查,还是仅使用Windows cmd.exe中的
type
命令?因为,如果文件不包含显式LF,
C:\where\>
提示符可能会在您看到标题之前覆盖它。

您需要给出正在使用的
html
示例。另外,您甚至没有看到
'producttitle'
标题吗?我已经添加了HTML。我也没有看到产品标题标题编号。我使用的是Windows和Python 3.4.3。您是否检查了正确的文件?看起来,除非有错误,否则您至少应该始终获取标题。当我运行脚本时,它正在创建文件,这可能是一个问题吗?