如何将 For 循环数据写入 CSV 文件

How To Write For Loop Data Into a CSV file

我正在构建一个 BOT,它可以抓取 ebay 上产品的价格并将所有信息写入 csv 文件。但是当它使用 for 循环将信息输入 csv 文件时我遇到了麻烦。如何将所有 for 循环数据写入 csv 文件。由于 for 循环,代码仅写入最后一个产品的信息。

headers = {'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36'}

searchterm = "Laptops"
url = f'https://www.ebay.com/sch/i.html?_from=R40&_nkw={searchterm}&_sacat=0&LH_PrefLoc=1&LH_Auction=1&rt=nc&LH_Sold=1&LH_Complete=1'

response = requests.get(url,headers=headers)
soup = BeautifulSoup(response.content,'html.parser')

for item in soup.select('.s-item'):
    print(len(soup.select('.s-item')))
    try:
        print(item.select('.s-item__title')[0].get_text())
        print(item.select('.s-item__subtitle')[0].get_text())
        print(item.select('.s-item__price')[0].get_text())
        print(item.select('.s-item__location')[0].get_text())
        print(item.select('.s-item__shipping')[0].get_text())
        print("\n\n\n----------------------------------------------")
        with open('Products.csv', mode='w') as file_in:
            writers = csv.writer(writers)
            employee_writer.writerow(['Product', 'Price'])
            employee_writer.writerow(['', ''])
            employee_writer.writerow([item.select('.s-item__title')[0].get_text(),item.select('.s-item__price')[0].get_text()])
    except Exception as e:
        print("ERROR!")

我希望表格看起来像 this

构造代码以便在循环外打开文件:

with open('Products.csv', mode='w') as file_in:
    writers = csv.writer(writers)
    for item in soup.select('.s-item'):
        # etc...

发生的事情是循环的每次迭代,您都在重新打开文件,并且 mode='w' 您正在覆盖文件内容。