Selenium Python - Access next pages of search results

前端 未结 2 1765
面向向阳花
面向向阳花 2021-01-21 14:15

I have to click on each search result one by one from this url:

Search Guidelines

I first extract the total number of results from the displayed text so that I c

2条回答
  •  梦毁少年i
    2021-01-21 14:24

    The problem is that the value of element with id total_results changes after the page is loaded, at first it contains 117, then changes to 44.

    Instead, here is a more robust approach. It processes page by page until there is no more pages left:

    from selenium import webdriver
    from selenium.common.exceptions import NoSuchElementException
    
    driver = webdriver.Firefox()
    url = 'http://www.nice.org.uk/Search.do?searchText=bevacizumab&newsearch=true#/search/?searchText=bevacizumab&mode=&staticTitle=false&SEARCHTYPE_all2=true&SEARCHTYPE_all1=&SEARCHTYPE=GUIDANCE&TOPICLVL0_all2=true&TOPICLVL0_all1=&HIDEFILTER=TOPICLVL1&HIDEFILTER=TOPICLVL2&TREATMENTS_all2=true&TREATMENTS_all1=&GUIDANCETYPE_all2=true&GUIDANCETYPE_all1=&STATUS_all2=true&STATUS_all1=&HIDEFILTER=EGAPREFERENCE&HIDEFILTER=TOPICLVL3&DATEFILTER_ALL=ALL&DATEFILTER_PREV=ALL&custom_date_from=&custom_date_to=11-06-2014&PAGINATIONURL=%2FSearch.do%3FsearchText%40%40bevacizumab%26newsearch%40%40true%26page%40%40&SORTORDER=BESTMATCH'
    driver.get(url)
    
    page_number = 1
    while True:
        try:
            link = driver.find_element_by_link_text(str(page_number))
        except NoSuchElementException:
            break
        link.click()
        print driver.current_url
        page_number += 1
    

    Basically, the idea here is to get the next page link, until there is no such ( NoSuchElementException would be thrown). Note that it would work for any number of pages and results.

    It prints:

    http://www.nice.org.uk/Search.do?searchText=bevacizumab&newsearch=true&page=1
    http://www.nice.org.uk/Search.do?searchText=bevacizumab&newsearch=true&page=2#showfilter
    http://www.nice.org.uk/Search.do?searchText=bevacizumab&newsearch=true&page=3#showfilter
    http://www.nice.org.uk/Search.do?searchText=bevacizumab&newsearch=true&page=4#showfilter
    http://www.nice.org.uk/Search.do?searchText=bevacizumab&newsearch=true&page=5#showfilter
    

提交回复
热议问题