Jolijt Tamanaha Jolijt Tamanaha - 1 year ago 140
Python Question

Scrape with BeautifulSoup from site that uses AJAX pagination using Python

I'm fairly new to coding and Python so I apologize if this is a silly question. I'd like a script that goes through all 19,000 search results pages and scrapes each page for all of the urls. I've got all of the scrapping working but can't figure out how to deal with the fact that the page uses AJAX to paginate. Usually I'd just make a loop with the url to capture each search result but that's not possible. Here's the page:

This is the script I have so far:

with'heritageURLs.txt', 'a', encoding='utf8') as logfile:
page = urllib2.urlopen("")
soup = BeautifulSoup(page)
snippet = soup.find_all('a', attrs={'item-title'})
for a in snippet:
logfile.write ("" + a.get('href') + "\n")

print "Done collecting urls"

Obviously, it scrapes the first page of results and nothing more.

And I have looked at a few related questions but none seem to use Python or at least not in a way that I can understand. Thank you in advance for your help.

Answer Source

For the sake of completeness, while you may try accessing the POST request and to find a way round to access to next page, like I suggested in my comment, if an alternative is possible, using Selenium will be quite easy to achieve what you want.

Here is a simple solution using Selenium for your question:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep

# uncomment if using Firefox web browser
driver = webdriver.Firefox()

# uncomment if using Phantomjs
#driver = webdriver.PhantomJS()

url = ''

# set initial page count
pages = 1
with open('heritageURLs.txt', 'w') as f:
    while True:
            # sleep here to allow time for page load
            # grab the Next button if it exists
            btn_next = driver.find_element_by_class_name('next')
            # find all item-title a href and write to file
            links = driver.find_elements_by_class_name('item-title')
            print "Page: {} -- {} urls to write...".format(pages, len(links))
            for link in links:
            # Exit if no more Next button is found, ie. last page
            if btn_next is None:
                print "crawling completed."
            # otherwise click the Next button and repeat crawling the urls
            pages += 1
        # you should specify the exception here
            print "Error found, crawling stopped"

Hope this helps.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download