简体   繁体   English

使用Selenium重定向到新页面后,如何刮擦新刷新的数据

[英]How do I Scrape new refreshed data after redirecting to the new page by using selenium

I'm Working on a data scraping work by using python and I wanted to do scrape the new redirect page data after clicking on the redirect button. 我正在使用python进行数据抓取工作,我想在单击重定向按钮后抓取新的重定向页面数据。

This is the code which i have tried. 这是我尝试过的代码。

browser =  webdriver.Firefox()
browser.get("https://www.cbsl.gov.lk/en/statistics/economic-indicators")
window_before = browser.window_handles[0]
print(window_before)
browser.find_element_by_xpath('/html/body/div[2]/div[3]/div/div/div/div/div/div/div/div/div/div/div/div/div/div/div/div[4]/div[2]/p[1]/a').click()
window_after = browser.window_handles[1]
browser.switch_to_window(window_after)
print(window_after)

bs_obj = BSoup(browser.page_source,'lxml')
table = bs_obj.find("table", id="statTB")
print(table)

this will redirect to the new page. 这将重定向到新页面。 but after print the table it was not showing anything. 但是在打印完表格后,它什么也没显示。 I think still it was trying on the old page. 我认为仍然在旧页面上尝试。

No. When you switched to new window, browser.page_source returns you HTML of new window, but you might need to wait until required table appeared in DOM: 否。当您切换到新窗口时, browser.page_source返回您新窗口的HTML,但是您可能需要等待直到所需table出现在DOM中:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

...
browser.switch_to_window(window_after)
table = WebDriverWait(browser, 10).until(EC.visibility_of_element_located((By.ID, "statTB")))
print(table.text)

you need multiple WebDriverWait , waiting second window and page loaded 您需要多个WebDriverWait ,等待第二个窗口和页面加载

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

browser.get("https://www.cbsl.gov.lk/en/statistics/economic-indicators")
window_before = browser.window_handles[0]
print(window_before)
browser.find_element_by_xpath('/html/body/div[2]/div[3]/div/div/div/div/div/div/div/div/div/div/div/div/div/div/div/div[4]/div[2]/p[1]/a').click()
WebDriverWait(browser, 20).until(EC.number_of_windows_to_be(2))
window_after = browser.window_handles[1]
browser.switch_to_window(window_after)
print(window_after)

myElem = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID, 'Grid')))
bs_obj = BeautifulSoup(browser.page_source, 'lxml')

table = bs_obj.find("table", id="statTB")
print(table)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从通过 selenium 和 python 提交数据后刷新的网页中抓取数据? - How do I scrape data from a web page that refreshes after submitting data via selenium and python? 如何使用 selenium 抓取新的弹出窗口? - How to scrape a new popup using selenium? 如何使用Selenium和Python抓取嵌套数据 - How do I scrape nested data using selenium and Python> 如何让 selenium 打开一个浏览器并继续使用该浏览器,而不是为每个 url/scrape javascript 加载一个新浏览器 - How do I get selenium to open one browser and keep using that browser instead of loading a new browser for every url / scrape javascript 如何使用 Python 仅抓取新链接(在上次抓取之后) - How to Scrape Only New Links (After Previous Scrape) Using Python 如何使用 Selenium 抓取这些数据? - How could I scrape this data using Selenium? 我如何使用下拉菜单来抓取网页? (使用硒) - How do i XPATH or CSS scrape a Web-Page by utilizing the drop-down menu? (Using Selenium) 如何使用 selenium 和 python 从动态生成的页面中抓取内容? - How do I scrape content from a dynamically generated page using selenium and python? 如何使用 selenium 抓取整个页面? - How do you scrape the whole page using selenium? 如何使用Selenium和Python在新选项卡中打开新链接(单击网页中的元素后生成)? - How to open the new link (generated after clicking an element in a web page) in a new tab using Selenium and Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM