我正在使用 selenium 从一个体育网站提取数据。我希望Chrome浏览器在我关闭之前一直保持打开状态。但我的程序在3-4秒后关闭了Chrome窗口。如果能帮助我找到解决方案,我将不胜感激。
这是我的密码
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import pandas as pd
print('\nWelcome to Arsenal FC players payroll page\n')
page_num = input('Enter the year for payroll data (2011-2020): ')
df = pd.DataFrame(columns = ['Player', 'Salary', 'Year']) #creates a master dataframe
driver = webdriver.Chrome('/Users/mahtabkhan/Documents/chromedriver')
if(page_num != 2020):
url = 'https://www.spotrac.com/epl/arsenal-fc/payroll/' + page_num + '/'
else:
url = 'https://www.spotrac.com/epl/arsenal-fc/payroll/'
driver.get(url)
players = driver.find_elements_by_xpath('//td[@class="player"]')
salaries = driver.find_elements_by_xpath('//td[@class="cap info"]')
#to get the text of each player into a list
players_list = []
for p in range(len(players)):
players_list.append(players[p].text)
#to get the salaries into a list
salaries_list = []
for s in range(len(salaries)):
salaries_list.append(salaries[s].text)
data_tuples = list(zip(players_list[1:],salaries_list[1:])) # list of each players name and salary paired together
temp_df = pd.DataFrame(data_tuples, columns=['Player','Salary']) # creates dataframe of each tuple in list
temp_df['Year'] = page_num # adds season beginning year to each dataframe
df = df.append(temp_df) #appends to master dataframe
driver.close()
4条答案
按热度按时间wrrgggsh1#
在您的WebDriver中(当您示例化它时),您可以将以下内容添加到Chrome选项中
完成后,通过命令终端(Windows中的命令提示符)运行它,它不会关闭
主程序-供参考
tgabmvqs2#
selenium 4 / PHP / Docker
$this->driver = RemoteWebDriver::createBySessionID(self::$session_id, self::$server, 60000, 60000);
zte4gxcn3#
driver.close()将关闭你的浏览器.只要删除它如果你想你的浏览器仍然打开
b4wnujal4#
它会关闭,因为你在末尾添加了
driver.close()
。只要删除该行,浏览器就会永远保持打开状态。如果你想在一段时间后关闭它,那么你可以在driver.close()
之前添加time.sleep
,如下所示: