【问题标题】:Button click before scraping an html table在抓取 html 表格之前单击按钮
【发布时间】: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


【解决方案1】:

试试下面的代码:

# Open the below URL
driver.get('https://datawarehouse.dbd.go.th')
# wait applied
driver.implicitly_wait(1)
# below line clicks the OK button on the pop-up
driver.find_element(By.ID, 'btnWarning').click()
# below line switches the language from TH to English
driver.find_element(By.XPATH, '//span[@class="slider round"]').click()
# Below line opens the required URL
driver.get('https://datawarehouse.dbd.go.th/company/profile/5/0245552001018')
# Below line clicks on Financial Information menu
button = driver.find_element(By.XPATH, '//span[@id="menu2"]')
button.click()
# Capture the HTML of the table
html = driver.find_element(By.XPATH, "//div[@class='card-body']/table").get_attribute('outerHTML')
print(html)
driver.quit()

我运行了上面的代码并且运行良好。下面是控制台输出:

<table class="table table-hover text-end table-fixed">
                <thead>
                    <tr class="text-center">
                        <th rowspan="2" class="align-middle col-fixed">Unit : Baht</th>         
                        <th colspan="2">2018</th>
                        <th colspan="2">2019</th>
                        <th colspan="2">2020</th>
                        <th colspan="2">2021</th>
                        <th colspan="2">2022</th>
                    </tr>
                    <tr class="text-center">                        
                        <th>Amount</th>
                        <th>%Change</th>  

                        <th>Amount</th>
                        <th>%Change</th>

                        <th>Amount</th>
                        <th>%Change</th>
                        
                        <th>Amount</th>
                        <th>%Change</th>
                        
                        <th>Amount</th>
                        <th>%Change</th>
                    </tr>
                </thead>
                
                <tbody>
                    <tr>
                        <th class="text-start col-fixed">Accounts Receivable</th> 
                        <td>342,438,019.00</td>
                        <td>12.63</td>

控制台输出很大,所以我只粘贴了它的一部分。

注意:我刚刚打印了 HTML,您可以将其转换为数据框。

【讨论】:

  • 谢谢肖恩!我尝试运行它,但出现错误。 (顺便说一句,我正在使用 Google Collab - 不确定这是否相关)这是错误:'ElementClickInterceptedException:消息:元素 <span class="slider round">​​ 在点 (1093,48) 不可点击,因为另一个元素 <div class="modal-backdrop fade"> 遮盖它'
  • 我还尝试禁用将语言从 TH 切换为英语的行。 (那是导致错误的原因。)然后,我得到了这个错误:'ElementClickInterceptedException: Message: Element <span id="menu2" class="tab2"> is not clickable at point (269,160) because another element <div class="preload"> 掩盖它'
猜你喜欢
  • 1970-01-01
  • 2018-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-20
相关资源
最近更新 更多