【发布时间】:2023-02-23 03:44:07
【问题描述】:
我正在尝试从网页中抓取 HTML 表格,您需要在表格显示之前先点击一个按钮。我试过这段代码,但我收到一条错误消息,说这样的按钮不存在。 (NoSuchElementException: Message: Unable to locate element) 有人可以给我一些指导吗?
这是我使用的代码:
# set up the Firefox webdriver
options = Options()
options.headless = True
driver = webdriver.Firefox(options=options)
# navigate to the website
driver.get('https://datawarehouse.dbd.go.th/company/profile/5/0245552001018')
# wait for the table to be loaded
driver.implicitly_wait(1) # wait for up to 1 second
#click button
button = driver.find_element("link text","Financial Information")
button.click()
# extract the HTML content of the table
html = driver.find_element("xpath", '//table').get_attribute('outerHTML')
# close the web browser
driver.quit()
# convert the HTML content to a pandas DataFrame
df = pd.read_html(html)[0]
# print the DataFrame
print(df)
【问题讨论】:
-
该网站的链接可能需要登录,因为它会重定向到主页。发布您要单击的按钮的相关 HTML。
标签: python pandas selenium-webdriver