简体   繁体   English

如何处理硒中的嵌套循环

[英]How to Handle Nested Loops in Selenium

I am hoping someone can help me handle nested loop in selenium. 我希望有人可以帮助我处理硒中的嵌套循环。 I am trying to Scrape a website using selenium, it happens that i have to scrape multiple information with different links. 我正在尝试使用硒抓取一个网站,碰巧我必须使用不同的链接抓取多个信息。

So i got all the links and looped through each, but in the process, the first link only displayed the items i needed, then the code breaks. 因此,我获得了所有链接并遍历了每个链接,但是在此过程中,第一个链接仅显示了我需要的项目,然后代码中断了。

 def get_financial_info(self):

    chrome_options = Options()
    chrome_options.add_argument("--headless")
    chrome_options.add_argument("--window-size=1920x1080")
    driver = webdriver.Chrome(chrome_options=chrome_options, executable_path='/home/miracle/chromedriver')

    driver.get("https://www.financialjuice.com")

    try:
        WebDriverWait(driver, 60).until(EC.visibility_of_element_located((By.XPATH, "//div[@class='trendWrap']")))
    except TimeoutException:
        driver.quit()

    category_url = driver.find_elements_by_xpath("//ul[@class='nav navbar-nav']/li[@class='text-uppercase']/a[@href]")
    for record in category_url:
        driver.get(record.get_attribute("href"))
        news = {}
        title_element = driver.find_elements_by_xpath("//p[@class='headline-title']")

        for news_record in title_element:
            news['title'] = news_record.text

            print news

Your category_url will be valid only on page where you've defined it and after first re-direction to another page it becomes stale... 您的category_url仅在您定义了它的页面上有效,并且在第一次重定向到另一个页面后,它变得陈旧...

You need to replace 您需要更换

category_url = driver.find_elements_by_xpath("//ul[@class='nav navbar-nav']/li[@class='text-uppercase']/a[@href]")

with

category_url = [a.get_attribute("href") for a in driver.find_elements_by_xpath("//ul[@class='nav navbar-nav']/li[@class='text-uppercase']/a")]

and then loop through the list of links as 然后遍历链接列表为

for record in category_url:
    driver.get(record)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM