为什么此功能不能在Selenium正常工作的情况下刮擦表?

 收藏

I'm trying to scrape the table on this website with Selenium (you need to make a quick account and login to see it). To get the first column, I use the following code, which works:

first_column = driver.find_elements_by_xpath("//div[@class='top-line-table']//tbody//td[2]")
for i in first_column:
    print(i.text)

印刷品:

Bernie Sanders
Joe Biden
Michael Bloomberg
Elizabeth Warren

但是,当我尝试使其成为可以输入所需列的函数时,它仅返回第一个值“ Bernie Sanders”。这是定义,调用和打印函数的代码:

def scrape_column (column):
raw = driver.find_elements_by_xpath(f"//div[@class='top-line-table']//tbody//td[{column}]")
for ii in raw:
        return[ii.text]

candidates = scrape_column("2")
print(candidates)

我不知道为什么它只返回第一个值,我已经尝试了很多方法,但仍然不起作用。非常感谢帮助!

回复