为了账号安全,请及时绑定邮箱和手机立即绑定

无法写入 csv 文件

无法写入 csv 文件

尚方宝剑之说 2023-09-12 18:28:31
当我尝试在 csv 文件中写入信息时,抛出错误:Traceback (most recent call last):File "sizeer.py", line 68, in <module>      writer.writerow([name,color,price])                     ValueError: I/O operation on closed fileimport requestsimport csvfrom bs4 import BeautifulSoupproxies = {    "http":"http://195.189.60.97:3128",     "http":"http://103.78.75.165:8080",    "http":"http://212.87.220.2:3128",    "http":"http://88.99.134.61:8080",    "http":"http://103.102.139.178:8080",    "http":"http://218.60.8.83:3129",    "http":"http://124.121.105.193:8888",    "http":"http://198.237.114.54:8080",    "http":"http://36.67.106.58:8080",    "http":"http://35.214.241.28:3128"}base_url = ...page = requests.get(base_url, proxies=proxies)if page.status_code != 200:    exit("Page wasn't parsed")soup = BeautifulSoup(page.content, 'lxml')with open("result.csv", "w") as file:    writer = csv.writer(file)    writer.writerow(["Product","Color","Price"])#Get categoriescategory_wrapper = soup.find_all(class_="m-menu_subItem")categories = []for cw in category_wrapper:    anchor = cw.find("a", recursive=False)    categories.append(anchor['href'])#Iterrate categoriesfor category in categories:    cat_page = requests.get(base_url + category, proxies=proxies)    cat_soup = BeautifulSoup(cat_page.content, 'lxml')    products_wrapper = cat_soup.find(class_="b-productList")    cat_pagination = products_wrapper.find(class_="m-pagination").find_all("span")    max_page = [int(s) for s in cat_pagination[-1].text.split() if s.isdigit()][0]    #Iterrate category with pagination and get products如何在整个脚本执行过程中保持文件打开?或者我每次添加内容时都必须打开它?编辑事实上,该文件甚至没有创建。
查看完整描述

1 回答

?
青春有我

TA贡献1784条经验 获得超8个赞

with open("result.csv", "w") as file:

    writer = csv.writer(file)

    writer.writerow(["Product","Color","Price"])

文件在with块的末尾关闭——这就是块的目的。


您可以将所有内容都放在块内,但这只会使现有问题变得更糟:代码达到了多层缩进,很长并且变得难以理解。这就是为什么使用函数来组织代码的原因。例如,如果您for在函数中设置了大循环:


def do_stuff_with(categories, writer):

    for category in categories:

        # lots of logic here

        # use `writer.writerow` when needed


# Get everything else set up that doesn't need the file, first

categories = ... # do the BeautifulSoup input stuff


# then we can open the file and use the function:

with open("result.csv", "w") as file:

    writer = csv.writer(file)

    writer.writerow(["Product","Color","Price"])

    do_stuff_with(categories, writer)

一旦你完成了这项工作,你可能就能想出进一步应用该技术的方法。例如,拉出最里面的逻辑,用于处理variations单个产品。或者你可以有一个函数来处理数据的创建categories,然后return它。


查看完整回答
反对 回复 2023-09-12
  • 1 回答
  • 0 关注
  • 410 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信