1

I am getting reviews for a webpage and need to scan each page until there are no longer any reviews left. Reviews page have multiple pages, and my first thought was to use a While loop, however, I am unsure where to start. HTML code of the web page looks something similar to this. enter image description here

HTML Code in last page;enter image description here

Any help is appreciated.

5
  • Show code you have tried so far. Also share HTML of the last page Commented Dec 25, 2016 at 11:45
  • Check if URL has page as a parameter. If it does, you don't need to use next button. And for parsing html I would suggest BeautifulSoup Commented Dec 25, 2016 at 11:52
  • URL doesn't have a page parameter. I couldn't come up with something for looping. Any code suggestions are very welcomed. @Andersson Commented Dec 25, 2016 at 11:59
  • Show HTML code of Next button on the very last page Commented Dec 25, 2016 at 12:10
  • On the very last page next button does not exist, therefore no HTML code for that. I just edited my question and added the html code for last page. @Andersson Commented Dec 25, 2016 at 12:13

1 Answer 1

5

Try to click Next while it can be found:

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException

driver = webdriver.Firefox()
driver.get("http://www.some_site.com")
while True:
    # do whatever you want
    try:
        driver.find_element_by_xpath('//a/span[text()="Next"]').click()
    except NoSuchElementException:
        break
Sign up to request clarification or add additional context in comments.

4 Comments

I am getting this error: NameError: global name 'driver' is not defined. I did not use Selenium before, my code uses urllib2 to request and BeautifulSoup to parse. @Andersson
Answer updated. If you use http requests to get page HTML source, why do you need selenium at all?
in order to use the html code browser-like, to click on next buttons. I could not find any other direct way to do this than Selenium.
Does my answer was helpful? Did you get rid of exception?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.