Skip to content

Instantly share code, notes, and snippets.

@bartenbach
Created December 20, 2017 01:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bartenbach/ef4140b7f34815ebbdf6428b018a24ae to your computer and use it in GitHub Desktop.
Save bartenbach/ef4140b7f34815ebbdf6428b018a24ae to your computer and use it in GitHub Desktop.
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import java.io.IOException;
import java.net.URLEncoder;
public final class Testing {
public static void main(String[] args) {
Connection.Response cResponse = null;
try {
String amazon = "http://www.amazon.com/s/ref=nb_sb_noss_2?url=search-alias%3Daps&field-keywords=";
String search = "practice locks";
cResponse = Jsoup.connect(amazon + URLEncoder.encode(search, "UTF-8"))
.userAgent("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36")
.referrer("http://www.google.com")
.followRedirects(true)
.execute();
Document doc = cResponse.parse();
Elements items = doc.getElementsByClass("s-result-item");
Elements ids = doc.getElementsByAttribute("data-asin");
System.out.println("Items: " + items.size());
for (int i = 0; i < items.size(); i++) {
System.out.println("ID: " + ids.attr("data-asin"));
Elements title = items.get(i).getElementsByClass("s-access-title");
if (title.size() > 0) {
String titleString = title.get(0).text();
System.out.println(titleString);
System.out.println("-----------------------------------------------");
} else {
System.out.println("no title");
}
}
} catch (IOException e) {
System.out.println("found no result");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment