Hi,
Is there any way to retrieve the contents of a DIV from another website,
E.G.
there is a div on Youtube with the ID description, how would i get the contents of that DIV?
Thanx
Sam Rudge

Dani AI

Generated

@samarrudge originally wanted the text inside a page element (a span with class description) available as a variable; correctly pointed out that the page source needs to be read, and later reported that an official service API solved the problem. The notes below cover robust, long-lived options for situations where an API is not available or when the page content is generated client-side.

A few practical rules of thumb:

  • Fetch pages server-side with an HTTP client that handles SSL, redirects and headers (avoid brittle plain string reads). Set a clear User-Agent, follow robots.txt and respect the site's terms of service and rate limits.
  • Parse HTML with a proper DOM parser and XPath rather than using ad-hoc string searches; HTML is not regular and small layout changes break naive approaches.

Example (PHP — parsing portion only; assume HTML is already retrieved into $html):

libxml_use_internal_errors(true);
$doc = new DOMDocument();
$doc->loadHTML($html);
libxml_clear_errors();

$xpath = new DOMXPath($doc);
$nodes = $xpath->query("//span[contains(concat(' ', normalize-space(@class), ' '), ' description ')]");
if ($nodes->length) {
    $description = trim($nodes->item(0)->textContent);
} else {
    $description = '';
}

If the text is injected by client-side JavaScript (not present in the server HTML), use the official API where available (most stable and lawful), or render the page with a headless browser (Puppeteer/Playwright) and then parse the resulting DOM. Always add error handling, text normalization (entities, whitespace), caching, and polite backoff to avoid hammering the source. This preserves correctness and keeps the solution maintainable over time.

Recommended Answers

All 7 Replies

Sory the info i want 2 get from youtube is in a span not div and the class is description not the id,
Well i think that first post was highly successful and accurate :$
Sam Rudge

Are you looking to write a script to get the contents of a div on another page for you or are you just interested in looking at them yourself?

I want to get the contents of the div and set it as a variable to use in my own script

I am also looking such code

I have not done this yet but I have an idea of how you would need to do it.
First you are going to need to use fopen to open the other page and place the contents of that page into a file for your script to read.
You will then need to search that file for the occurrence of the specific div span element that you are looking for. I hope for your sake that the span you are looking for has a specific name and not just span hehe. Anyways you do the string search find what you are looking for and then slap that into your variable.

Helpful links:
http://us2.php.net/manual/en/function.fopen.php
http://us2.php.net/fread
http://us.php.net/strings

Since I do not know the specifics of what you want to do in terms of finding the term the third link is to all PHP string functions.

If it wasn't already obvious to you, you would need to read the source code of the page.

Its OK now, thankyou for your help but i use the google/youtube API and found it much easier than searching and opening

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.