我正在编写一个程序,用于抓取网站并从表格中提取名称和链接。我将每个名称和相应的链接存储在一个对象中,并通过运行 for 循环将其附加到对象列表中。我想将此列表保存为 csv 文件,但无法使用 filewriter 这样做,因为它将整个对象转换为 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 …Run Code Online (Sandbox Code Playgroud)