I'm working with some webscraping using Mathematica and have run into a question: is it possible to grab all the links on a webpage that scrolls down? The page I am trying to scrape is this: "http://store.nike.com/us/en_us/pw/mens-tops-t-shirts/7puZobp?ipp=120".
I have created a scraper using XMLObjects and Cases with the following code:
(*Open XML Tree*)
urlShirtsPage =
"http://store.nike.com/us/en_us/pw/mens-tops-t-shirts/7puZobp?ipp=\
120";
getXml[anyUrl_] := (
current = urlShirtsPage;
page = URLFetch[urlShirtsPage];
If[StringLength@page == 0, getXml@current,
Return@ImportString[page, "XMLObject"]]);
largeXml = getXml[urlShirtsPage];
(*Grab All Links*)
firstScreenLinks = Cases[largeXml,
XMLElement[
"div", {"class" -> "grid-item fullSize", "data-pdpurl" -> ___,
"data-column-index" -> __, "data-item-index" -> __}, {___}],
Infinity];
allLinks = secondScreenLinks = Cases[#,
XMLElement[
"div", {"class" -> "grid-item fullSize",
"data-pdpurl" -> links_, "data-column-index" -> __,
"data-item-index" -> __}, {___}] -> links, 1] &@
firstScreenLinks
Length@allLinks
The code above basically scrapes the URLs of Nike Shirts products that are displayed in columns on the webpage. The only problem is, it only returns 120 URLs when there are 855 products (each with its own respective URLs).
Using the Chrome Developer Tools, I can see that when I scroll a certain distance the webpage pulls up another script that displays additional shirts. I locate the URL that pulls up the new script (using Developer Tools), and Import it into Mathematica using:
urls = "http://store.nike.com/html-services/gridwallData?country=US&\
lang_locale=en_US&gridwallPath=mens-tops-t-shirts/7puZobp&pn=2";
Import[urls, "Text"]
The code above indicates that the new URLs being pulled up are not in the first scraper above, which I assume means it was loaded while I was scrolling down.
I am wondering if there is a faster way to pull up all the URLs on the page (loaded and not yet loaded) rather than having to keep scrolling, checking for new scripts to appear, and importing and scraping the new URLs on Mathematica. As there are over 800 links, it would take me a very long time to perform the latter process. Any insight would be greatly appreciated!
Select[ Import["http://store.nike.com/us/en_us/pw/mens-tops-t-shirts/7puZobp?ipp=120", "Hyperlinks"], StringContainsQ["pid-"] ]
I get a list of over 500 URLs $\endgroup$