无法写入 csv 文件

当我尝试在 csv 文件中写入信息时,抛出错误:


Traceback (most recent call last):

File "sizeer.py", line 68, in <module> 

     writer.writerow([name,color,price])                     

ValueError: I/O operation on closed file

import requests

import csv

from bs4 import BeautifulSoup


proxies = {

    "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 categories

category_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 categories

for 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

如何在整个脚本执行过程中保持文件打开?或者我每次添加内容时都必须打开它?编辑事实上,该文件甚至没有创建。


尚方宝剑之说
浏览 411回答 1
1回答

青春有我

with open("result.csv", "w") as file:&nbsp; &nbsp; writer = csv.writer(file)&nbsp; &nbsp; writer.writerow(["Product","Color","Price"])文件在with块的末尾关闭——这就是块的目的。您可以将所有内容都放在块内,但这只会使现有问题变得更糟:代码达到了多层缩进,很长并且变得难以理解。这就是为什么使用函数来组织代码的原因。例如,如果您for在函数中设置了大循环:def do_stuff_with(categories, writer):&nbsp; &nbsp; for category in categories:&nbsp; &nbsp; &nbsp; &nbsp; # lots of logic here&nbsp; &nbsp; &nbsp; &nbsp; # use `writer.writerow` when needed# Get everything else set up that doesn't need the file, firstcategories = ... # do the BeautifulSoup input stuff# then we can open the file and use the function:with open("result.csv", "w") as file:&nbsp; &nbsp; writer = csv.writer(file)&nbsp; &nbsp; writer.writerow(["Product","Color","Price"])&nbsp; &nbsp; do_stuff_with(categories, writer)一旦你完成了这项工作,你可能就能想出进一步应用该技术的方法。例如,拉出最里面的逻辑,用于处理variations单个产品。或者你可以有一个函数来处理数据的创建categories,然后return它。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python