首页 > 解决方案 > 如何从动态网页中提取信息

问题描述

单击查看详细信息的绿色按钮后,我会弹出显示更多信息的窗口。我怎样才能刮掉那个弹出窗口中的信息。单击使用 selenium 后,我尝试提取信息,但我收到此错误

标签: seleniumscrapy

解决方案


您在那里有一个 iframe,它会导致问题:您可以从该弹出窗口中获取数据,如下所示:

from selenium import webdriver
import time 

driver = webdriver.Chrome(executable_path="D:/chromedriver.exe")

driver.get('http://www.directoriomedicomexicano.com/Esp_Medicas/MedicosListado.aspx?P1=37&P2=ORTOPEDIA%20Y%20TRAUMATOLOGIA') 

#get all 'Ver detalles'
view_more_buttons = driver.find_elements_by_xpath("//img[contains(@onclick,'ShowWindow')]")

#click on the first one
view_more_buttons[0].click()

#switch to the iframe
iframe = driver.find_element_by_id("ctl00_ContentPlaceHolder1_ASPxPopupControl1_CIF1")
driver.switch_to.frame(iframe)

#wait 3 seconds
time.sleep(3)

#get the text from the table
table = driver.find_element_by_xpath("//table[@align='center']")

print(table.text)

在此处输入图像描述


推荐阅读