Scraping images from a webpage
in
Programming Questions
•
1 year ago
I need to scrape images from Google Image search results for a little project I'm doing. How would I go about doing this? I already tried proHTML, but that didn't return any images even though there were some on the webpage. Right now I'm trying XMLElement with this code, but I'm getting nullpointers at image().
- XMLElement xml;
- PImage[] imgs = new PImage[1];
- void setup() {
- size(200, 200);
- String[] pageRaw = loadStrings("http://google.com/m/search?site=images");
- String pageOne = "";
- for(int i=0; i < pageRaw.length; i++) {
- pageOne += pageRaw[i];
- }
- pageOne = pageOne.replaceAll("©","(c)");
- pageOne = pageOne.replaceAll(" "," ");
- saveStrings("pageData.txt",new String[] {pageOne});
- xml = new XMLElement(this, "../pageData.txt");
- printRecursive(xml);
- //imgs[0] = loadImage("http://www.gstatic.com/m/images/google_logo_100.gif");
- }
- void draw() {
- background(255);
- image(imgs[0],0,0);
- }
- XMLElement printRecursive(XMLElement x) {
- if(x.getChildCount() == 0) {
- return null;
- } else {
- println(x); //debug
- if((x.getName() == "img") && (x.getString("src","")!= "")) {
- imgs = (PImage[]) append(imgs,loadImage(x.getString("src")));
- }
- for(int i=0; i < x.getChildCount(); i++) {
- printRecursive(x.getChild(i));
- }
- return x;
- }
- }
1