将对象列表存储为 python 中的 csv

Storing a list of objects as csv in python

我正在编写一个程序,用于抓取网站并从 table 中提取名称和 link。我将每个名称和各自的 link 存储在一个对象中,并通过 运行 一个 for 循环将其附加到对象列表中。我想将此列表保存为 csv 文件,但无法使用文件编写器将整个对象转换为 str,这不是我想要的。如果有人可以指导我,我将不胜感激。这是我的代码:

from selenium import webdriver
from bs4 import BeautifulSoup

class Player():
    def __init__(self):
        self.name = ""
        self.link = ""

player_list = []

driver = webdriver.PhantomJS(executable_path = r'C:\Users\sarim\Desktop\Scraper\phantomjs.exe')
driver.get('https://dak.gg/ranks/na/solo-fpp/rating')
pause = 0
lastHeight = driver.execute_script("return document.body.scrollHeight")
while True:
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
    time.sleep(pause)
    newHeight = driver.execute_script("return document.body.scrollHeight")
    if newHeight == lastHeight:
        break
    lastHeight = newHeight

html_doc = driver.page_source
soup = BeautifulSoup(html_doc, 'lxml')

players = soup.find('table', class_ = 'list rating')
player_names = players.find_all('td', class_ = 'nick')

add_link = "https://dak.gg"

for i in player_names[0:3]:
    newPlay = Player()
    n = i.find('a')
    l = add_link + n['href']
    newPlay.link = l
    newPlay.name = n.text.strip()
    #print newPlay.name
    #print newPlay.link
    player_list.append(newPlay)

#print players_names[1]
driver.quit()

尝试这样的事情

import csv
with open('data.csv', 'w',) as csvfile:
    writer = csv.writer(csvfile)
    writer.writerow(['Name', 'Link'])
    for player in player_list:
        writer.writerow([player.name, player.link])