如何在Python中处理Iframe的多个页面?

2024-04-27 04:56:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我在用硒擦一些枣。一切似乎都很顺利。但问题是,在尝试使用循环跨iFrame的多个页面收集数据时,我遇到了一个问题driver.page_source只返回第一个页面的数据。因此,循环在iframe页面之间切换,但源代码保持不变。你知道吗

    ...
    iframe = driver.find_elements_by_tag_name('iframe')[0] #find data iframe
    driver.switch_to_frame(iframe) #switch to iframe
    pageo = 1
    while len(data)<numo:
        if pageo != 1:
            #continue
            link = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH,  '''//a[@onclick='set_current_page("'''+str(pageo) + '''");']''')))
            link.click()
            time.sleep(2)
            #driver.find_element_by_xpath(('''//a[@onclick='set_current_page("'''+str(pageo) + '''");']''')).click()
        #time.sleep(0.5)
        html_source = driver.page_source
        soup = BeautifulSoup(html_source, "html.parser")
        table = soup.findAll('table')[1]
        table_body = table.find('tbody')
        rows = table_body.find_all('tr')
        for row in rows:
            cols = row.find_all('td')
            cols = [ele.text.strip() for ele in cols]
            data.append([ele for ele in cols if ele])
            dynamic_data_entry()
        pageo = pageo + 1
    ...

这里有什么问题?你知道吗


Tags: 数据insourcefordatahtmldriverpage