/** * @return true if the image was successfully downloaded */ public boolean isComplete() { return isComplete_ || (hasFeature(JS_IMAGE_COMPLETE_RETURNS_TRUE_FOR_NO_REQUEST) ? ATTRIBUTE_NOT_DEFINED == getSrcAttribute() : imageData_ != null); }
/** * @return true if the image was successfully downloaded */ public boolean isComplete() { return isComplete_ || (hasFeature(JS_IMAGE_COMPLETE_RETURNS_TRUE_FOR_NO_REQUEST) ? ATTRIBUTE_NOT_DEFINED == getSrcAttribute() : imageData_ != null); }
/** * Makes sure that all the images in the page loads successfully. * (By default, HtmlUnit doesn't load images.) */ public void assertAllImageLoadSuccessfully(HtmlPage p) { for (HtmlImage img : DomNodeUtil.<HtmlImage>selectNodes(p, "//IMG")) { try { img.getHeight(); } catch (IOException e) { throw new Error("Failed to load "+img.getSrcAttribute(),e); } } }
/** * Makes sure that all the images in the page loads successfully. * (By default, HtmlUnit doesn't load images.) */ public void assertAllImageLoadSuccessfully(HtmlPage p) { for (HtmlImage img : DomNodeUtil.<HtmlImage>selectNodes(p, "//IMG")) { try { img.getHeight(); } catch (IOException e) { throw new Error("Failed to load "+img.getSrcAttribute(),e); } } }
HtmlElement element4 = null; Iterable<HtmlElement> iterable5 = page.getAllHtmlChildElements(); Iterator<HtmlElement> i6 = iterable5.iterator(); while(i6.hasNext() { HtmlElement anElement = i6.next(); if(anElement instanceof HtmlImage) { HtmlImage input = (HtmlImage) anElement; String[] elements = "http://example.com/pages/powerbutton.png".split( "/" ); if(input.getSrcAttribute().indexOf(elements[elements.length-1] )> -1 ){ element4 = input; break; } } } HtmlPage page = element4.click();
HtmlElement element4 = null; Iterable<HtmlElement> iterable5 = page.getAllHtmlChildElements(); Iterator<HtmlElement> i6 = iterable5.iterator(); while(i6.hasNext()) { HtmlElement anElement = i6.next(); if(anElement instanceof HtmlImage) { HtmlImage input = (HtmlImage) anElement; String[] elements = "http://example.com/pages/powerbutton.png".split( "/" ); if(input.getSrcAttribute().indexOf(elements[elements.length-1] )> -1 ) { element4 = input; break; } } } HtmlPage page = element4.click();
import java.net.URL; import java.util.List; import com.gargoylesoftware.htmlunit.WebClient; import com.gargoylesoftware.htmlunit.html.HtmlImage; import com.gargoylesoftware.htmlunit.html.HtmlPage; public class urlscrap { public static void main(String[] args) throws Exception { //WebClient webClient = new WebClient(Opera); WebClient webClient = new WebClient(); HtmlPage currentPage = (HtmlPage) webClient.getPage(new URL("http://www.google.com")); //get list of all divs final List<?> images = currentPage.getByXPath("//img"); for (Object imageObject : images) { HtmlImage image = (HtmlImage) imageObject; System.out.println(image.getSrcAttribute()); } //webClient.closeAllWindows(); } }
public Main() throws Exception { WebClient webClient = new WebClient(); webClient.setJavaScriptEnabled(false); HtmlPage page = webClient.getPage("http://www.chapitre.com"); List<HtmlImage> imageList = (List<HtmlImage>) page.getByXPath("//img"); for (HtmlImage image : imageList) { try { new URL(image.getSrcAttribute()); if (image.getHeightAttribute().equals("1") && image.getWidthAttribute().equals("1")) { System.out.println(image.getSrcAttribute()); } } catch (Exception e) { System.out.println("You didn't see this comming :)"); } } }
private void readImageIfNeeded() throws IOException { downloadImageIfNeeded(); if (imageData_ == null) { if (null == imageWebResponse_) { throw new IOException("No image response available (src=" + getSrcAttribute() + ")"); } @SuppressWarnings("resource") final ImageInputStream iis = ImageIO.createImageInputStream(imageWebResponse_.getContentAsStream()); final Iterator<ImageReader> iter = ImageIO.getImageReaders(iis); if (!iter.hasNext()) { iis.close(); throw new IOException("No image detected in response"); } final ImageReader imageReader = iter.next(); imageReader.setInput(iis); imageData_ = new ImageData(imageReader); // dispose all others while (iter.hasNext()) { iter.next().dispose(); } } }
/** * Returns the value of the <tt>src</tt> attribute. * @return the value of the <tt>src</tt> attribute */ public String jsxGet_src() { final HtmlImage img = (HtmlImage) getDomNodeOrDie(); final String src = img.getSrcAttribute(); if (instantiatedViaJavaScript_ && "".equals(src)) { return src; } try { final HtmlPage page = (HtmlPage) img.getPage(); return page.getFullyQualifiedUrl(src).toExternalForm(); } catch (final MalformedURLException e) { final String msg = "Unable to create fully qualified URL for src attribute of image " + e.getMessage(); throw Context.reportRuntimeError(msg); } }
/** * Returns the value of the <tt>src</tt> attribute. * @return the value of the <tt>src</tt> attribute */ public String jsxGet_src() { final HtmlImage img = (HtmlImage) getDomNodeOrDie(); final String src = img.getSrcAttribute(); if (instantiatedViaJavaScript_ && "".equals(src)) { return src; } try { final HtmlPage page = (HtmlPage) img.getPage(); return page.getFullyQualifiedUrl(src).toExternalForm(); } catch (final MalformedURLException e) { final String msg = "Unable to create fully qualified URL for src attribute of image " + e.getMessage(); throw Context.reportRuntimeError(msg); } }
/** * Returns the value of the <tt>src</tt> attribute. * @return the value of the <tt>src</tt> attribute */ public String jsxGet_src() { final HtmlImage img = (HtmlImage) getDomNodeOrDie(); final String src = img.getSrcAttribute(); if (instantiatedViaJavaScript_ && "".equals(src)) { return src; } try { final HtmlPage page = (HtmlPage) img.getPage(); return page.getFullyQualifiedUrl(src).toExternalForm(); } catch (final MalformedURLException e) { final String msg = "Unable to create fully qualified URL for src attribute of image " + e.getMessage(); throw Context.reportRuntimeError(msg); } }
private void readImageIfNeeded() throws IOException { downloadImageIfNeeded(); if (imageData_ == null) { if (null == imageWebResponse_) { throw new IOException("No image response available (src=" + getSrcAttribute() + ")"); } @SuppressWarnings("resource") final ImageInputStream iis = ImageIO.createImageInputStream(imageWebResponse_.getContentAsStream()); final Iterator<ImageReader> iter = ImageIO.getImageReaders(iis); if (!iter.hasNext()) { iis.close(); throw new IOException("No image detected in response"); } final ImageReader imageReader = iter.next(); imageReader.setInput(iis); imageData_ = new ImageData(imageReader); // dispose all others while (iter.hasNext()) { iter.next().dispose(); } } }
/** * {@inheritDoc} */ @Override public boolean isDisplayed() { final String src = getSrcAttribute(); if (hasFeature(HTMLIMAGE_INVISIBLE_NO_SRC)) { if (ATTRIBUTE_NOT_DEFINED == src) { return false; } if (hasFeature(HTMLIMAGE_BLANK_SRC_AS_EMPTY) && StringUtils.isBlank(src)) { return false; } if (hasFeature(HTMLIMAGE_EMPTY_SRC_DISPLAY_FALSE) && StringUtils.isEmpty(src)) { return false; } } return super.isDisplayed(); }
/** * Returns the value of the {@code src} attribute. * @return the value of the {@code src} attribute */ @JsxGetter public String getSrc() { final HtmlImage img = (HtmlImage) getDomNodeOrDie(); final String src = img.getSrcAttribute(); if ("".equals(src)) { return src; } try { final HtmlPage page = (HtmlPage) img.getPage(); return page.getFullyQualifiedUrl(src).toExternalForm(); } catch (final MalformedURLException e) { final String msg = "Unable to create fully qualified URL for src attribute of image " + e.getMessage(); throw Context.reportRuntimeError(msg); } }
/** * {@inheritDoc} */ @Override public boolean isDisplayed() { final String src = getSrcAttribute(); if (hasFeature(HTMLIMAGE_INVISIBLE_NO_SRC)) { if (ATTRIBUTE_NOT_DEFINED == src) { return false; } if (hasFeature(HTMLIMAGE_BLANK_SRC_AS_EMPTY) && StringUtils.isBlank(src)) { return false; } if (hasFeature(HTMLIMAGE_EMPTY_SRC_DISPLAY_FALSE) && StringUtils.isEmpty(src)) { return false; } } return super.isDisplayed(); }
/** * Returns the value of the {@code src} attribute. * @return the value of the {@code src} attribute */ @JsxGetter public String getSrc() { final HtmlImage img = (HtmlImage) getDomNodeOrDie(); final String src = img.getSrcAttribute(); if ("".equals(src)) { return src; } try { final HtmlPage page = (HtmlPage) img.getPage(); return page.getFullyQualifiedUrl(src).toExternalForm(); } catch (final MalformedURLException e) { final String msg = "Unable to create fully qualified URL for src attribute of image " + e.getMessage(); throw Context.reportRuntimeError(msg); } }
/** * <span style="color:red">POTENIAL PERFORMANCE KILLER - DOWNLOADS THE IMAGE - USE AT YOUR OWN RISK.</span><br/> * If the image is not already downloaded it triggers a download. Then it stores the image in the HtmlImage * object for later use.<br/> * * Downloads the image specified in the src attribute. * * @throws IOException if an error occurs while downloading the image or if the stream is of an * unsupported content-type */ private void downloadImageIfNeeded() throws IOException { if (!downloaded_) { final HtmlPage page = (HtmlPage) getPage(); final WebClient webclient = page.getWebClient(); final URL url = page.getFullyQualifiedUrl(getSrcAttribute()); final WebRequestSettings request = new WebRequestSettings(url); request.setAdditionalHeader("Referer", page.getWebResponse().getRequestSettings().getUrl().toExternalForm()); imageWebResponse_ = webclient.loadWebResponse(request); downloaded_ = true; } }
/** * <span style="color:red">POTENIAL PERFORMANCE KILLER - DOWNLOADS THE IMAGE - USE AT YOUR OWN RISK.</span><br/> * If the image is not already downloaded it triggers a download. Then it stores the image in the HtmlImage * object for later use.<br/> * * Downloads the image specified in the src attribute. * * @throws IOException if an error occurs while downloading the image or if the stream is of an * unsupported content-type */ private void downloadImageIfNeeded() throws IOException { if (!downloaded_) { final HtmlPage page = (HtmlPage) getPage(); final WebClient webclient = page.getWebClient(); final URL url = page.getFullyQualifiedUrl(getSrcAttribute()); final WebRequestSettings request = new WebRequestSettings(url); request.setAdditionalHeader("Referer", page.getWebResponse().getRequestSettings().getUrl().toExternalForm()); imageWebResponse_ = webclient.loadWebResponse(request); downloaded_ = true; } }
/** * <span style="color:red">POTENIAL PERFORMANCE KILLER - DOWNLOADS THE IMAGE - USE AT YOUR OWN RISK.</span><br/> * If the image is not already downloaded it triggers a download. Then it stores the image in the HtmlImage * object for later use.<br/> * * Downloads the image specified in the src attribute. * * @throws IOException if an error occurs while downloading the image or if the stream is of an * unsupported content-type */ private void downloadImageIfNeeded() throws IOException { if (!downloaded_) { final HtmlPage page = (HtmlPage) getPage(); final WebClient webclient = page.getWebClient(); final URL url = page.getFullyQualifiedUrl(getSrcAttribute()); final WebRequestSettings request = new WebRequestSettings(url); request.setAdditionalHeader("Referer", page.getWebResponse().getRequestSettings().getUrl().toExternalForm()); imageWebResponse_ = webclient.loadWebResponse(request); downloaded_ = true; } }