How can I extract all img tag within an anchor tag?

Asked
Active3 hr before
Viewed126 times

8 Answers

extract
90%

I am trying it with the code below but its getting all anchor tag and making it's text empty due the inside of an img tag.,I would like to extract all img tags that are within an anchor tag using the PHP DOM object., 1 @J.J. If loop over the result, you'll have only those anchors, that have an image inside and you get those anchors as DOMNodes, so you can add an ID or a class to identify them further. (Did you even tried the above?) – feeela Sep 14 '11 at 11:39 , By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

$xml = new DOMDocument;
libxml_use_internal_errors(true);
$xml - > loadHTMLFile($url);
libxml_clear_errors();
libxml_use_internal_errors(false);
$xpath = new DOMXPath($xml);
foreach($xpath - > query('//a[contains(@href, "www")]/img') as $entry) {
   var_dump($entry - > getAttribute('src'));
}
load more v
88%

<!DOCTYPE html>
<html>

<head>
   <title>HTML Image as link</title>
</head>

<body>
   The following image works as a link:<br>
   <a href="https://www.qries.com/">
      <img alt="Qries" src="https://www.qries.com/images/banner_logo.png" width=150" height="70">
   </a>
</body>

</html>
load more v
72%

To use image as a link in HTML, use the <img> tag as well as the <a> tag with the href attribute. The <img> tag is for using an image in a web page and the <a> tag is for adding a link. Under the image tag src attribute, add the URL of the image. With that, also add the height and width.,You can try to run the following code to use an image as a link in HTML,How to use an image in a webpage?,How to use an animated image in HTML page?

<!DOCTYPE html>
<html>
   

<head>
         <title>HTML Image as link</title>
      </head>
   

<body>
         The following image works as a link:<br>
         <a href="https://www.qries.com/">
               <img alt="Qries" src="https://www.qries.com/images/banner_logo.png"          width=150" height="70">
            </a>
      </body>

</html>
65%

Selecting attribute and save it’s content to a variable.,Example 2: This example displays the attribute of an image object.,How to Remove Special Character from String in PHP ?,Selecting each image in that document.

75%

Once in the shell we can try selecting all <link> objects and see that it doesn’t work (because the Atom XML namespace is obfuscating those nodes):,response is an HtmlResponse or an XmlResponse object that will be used for selecting and extracting data.,Keep in mind that if you are nesting selectors and use an XPath that starts with /, that XPath will be absolute to the document and not relative to the Selector you’re calling it from.,Since we’re dealing with HTML, the selector will automatically use an HTML parser.

>>> response.selector.xpath('//span/text()').get()
'good'
load more v
40%

I wish to extract all images from my database that stored using WYSIWYG to server.,The data are stored in the format such as record1:,I have some problem dealing with extracting image to server.,The records do indeed come from a database. And your script work great btw

<?php

$doc = new DOMDocument();
$doc->loadHTML('<img src="book_cover.jpg">the description is here.<img src="image2.jpg">continue the story here');
$xml = simplexml_import_dom($doc);
$images = $xml->xpath('//img');
foreach ($images as $img)
{
    echo $img['src'] . "<br />";
}


load more v
22%

tagname: find elements by tag, e.g. a,ns|tag: find elements by tag in a namespace, e.g. fb|name finds <fb:name> elements,#id: find elements by ID, e.g. #logo,.class: find elements by class name, e.g. .masthead

File input = new File("/tmp/input.html");
Document doc = Jsoup.parse(input, "UTF-8", "http://example.com/");

Elements links = doc.select("a[href]"); // a with href
Elements pngs = doc.select("img[src$=.png]");
// img with src ending .png

Element masthead = doc.select("div.masthead").first();
// div with class=masthead

Elements resultLinks = doc.select("h3.r > a"); // direct a after h3
60%

Inside a <script>, <link>, or <img> tag, PyCharm suggests completion for the path to the file you are referencing.,To preview an image in a popup instead of in a separate tab, select the reference to it and press Ctrl+Shift+I,Alternatively, position the caret at the reference to the image in the editor and press Ctrl+B,You can configure HTML-aware syntax highlighting according to your preferences and habits.

Create references in an HTML file

Inside a <script>, <link>, or <img> tag, PyCharm suggests completion for the path to the file you are referencing.

<script>

Create references in an HTML file

Inside a <script>, <link>, or <img> tag, PyCharm suggests completion for the path to the file you are referencing.

<link>

Create references in an HTML file

Inside a <script>, <link>, or <img> tag, PyCharm suggests completion for the path to the file you are referencing.

<img>
load more v

Other "extract-undefined" queries related to "How can I extract all img tag within an anchor tag?"