【发布时间】:2020-04-14 18:47:02
【问题描述】:
我能够使用 selenium 在我的 Chrome 网络浏览器的单独选项卡中打开一个网页。这个网页有一个庞大的表格数据,我想通过beautifulsoup 获取。但是,我的代码生成了以下错误消息。我的代码正在 Windows 上运行。
selenium.common.exceptions.NoSuchWindowException: Message: no such window: target window already closed
from unknown error: web view not found
以下是我一直在使用的代码。
从硒导入网络驱动程序 从 bs4 导入 BeautifulSoup 将熊猫导入为 pd 导入请求 导入操作系统 进口时间
#Open Webpage
url = "https://www.example.com"
driver=webdriver.Chrome(executable_path=r"C:\Users\Ben Lutz\Desktop\Ben Lutz\Runway Data Project\Updated Data\New Sheets\Scripts\chromedriver.exe")
driver.get(url)
driver.find_element_by_partial_link_text('Run').click()
time.sleep(100)
driver.close()
#Scrape Data
df_url = driver.current_url #This is the part of the code that the error is highlighting
page = requests.get(df_url).text
soup = BeautifulSoup(page, features = 'lxml')
text = soup.get_text()
soup.prettify()
table = soup.find('table')
table_rows = table.find_all('tr')
#print(table_rows)
Operations = pd.DataFrame()
for tr in table_rows:
td = tr.find_all('td')
row = [i.text for i in td]
df_row = pd.DataFrame(row).T
Operations = Operations.append(df_row.iloc[0])
print(Operations.head)
我产生这个错误是有原因的吗?应该如何解决这个困境?
任何帮助都是真正有帮助的。谢谢。
【问题讨论】:
标签: python python-3.x selenium beautifulsoup