Youtube Scraper

Another scraper, this time for :

USAGE:

1
<?php youtube("BMW") ?>

CODE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php
function youtube($keyword) {
	$url = 'http://www.youtube.com/rss/tag/'.urlencode($keyword).'.rss';
	$youtube = fopen($url, "r");
	if (preg_match('/<enclosure url=\"(.*)swf/s', $youtube, $y)) {
		$youtube = $y[1];
		$youtube = substr($y[1], 0, 36);
		$video = '';
		$video .= "\n".'<object type="application/x-shockwave-flash" style="width:400px; height:325px;" data="'.$youtube.'">';
    	$video .= "\n".'<param name="movie" value="'.$youtube.'" />';
    	$video .= "\n".'</object>';
    	$video .= "\n";
    	print $video;
	}
}
?>

Similar Posts:

13 Responses to “Youtube Scraper”


  • uhm.. doesn’t work for me..
    I get a blank page..

  • uhm.. doesn’t work for me..
    I get a blank page..

    Nils you are right, I was missing this line:
    $youtube = fopen($url, “r”);
    I just updated the code with the fix.

  • Thanks for the code…I am anxious to use it!

    Is this code working correctly? I am getting a blank page and
    am not sure if it is an error that I made or if the code still
    needs more updating.

    There is a very good chance that I messed up so forgive me if
    it is my fault :)

  • Warning: preg_match() expects parameter 2 to be string, resource given in /home/public_html/004/youtube.php on line 5

    I’m getting this error.

  • I don’t have any idea of how to use this script. Can you please give us some detailed instruction for doing it? I have messaged you on Digital point forum about this. Please guide me.

  • I don’t have any idea of how to use this script. Can you please give us some detailed instruction for doing it? I have messaged you on Digital point forum about this. Please guide me.

    It’s actually really easy, just copy the code and save it on a file (youtube.php for example). Then wherever you want to insert a Youtube video related to a keyword, just add this:

    require_once(‘youtube.php’); // assuming your file is called youtube.php

    And then just wherever you want to see the video add this:

    youtube(‘bmw’); // this will include a youtube video about BWM

  • Will this make my blog automated? Or should I have to add any cronjob or something to control the frequency of updating my blog?

  • No it wont post automatically but will give your site or page dynamic content.

  • Thanks for this. I’ve been wondering how to get something like this working for a while.

    Wayne

  • Thanks for the script, that will come in handy on a new project that I am working on. =)

  • This code doesn’t work.Just gives me a blank page :(

  • have not done many PHP projects but I always think that PHP is really good for web programming even though I mostly program in Java and .NET.

    I am not a good PHP programmer, but to write a simple web page scraper in PHP is very straightforward.

    First, I defined my HTTP get/post function

    function http_post_content($url,$data) {
    $data = http_build_query($data);
    $aContext = array(
    ‘http’ => array(
    ‘proxy’ => ‘proxyserver:8080′,
    ‘request_fulluri’ => True,
    ‘method’=>’POST’,
    ‘header’=>
    “Content-type: application/x-www-form-urlencoded\r\n”
    .”Content-Length: ” . strlen($data) . “\r\n”,
    ‘content’ => $data
    ),
    );
    $cxContext = stream_context_create($aContext);
    $content = file_get_contents($url, FILE_TEXT, $cxContext);
    return $content;

    /*
    $fp = @fopen($url, ‘rb’, false, $cxContext);
    if (!$fp) {
    throw new Exception(“Problem with $url, $php_errormsg”);
    }
    $content = @stream_get_contents($fp);
    if ($content === false) {
    throw new Exception(“Problem reading data
    from $url, $php_errormsg”);
    }
    */
    }
    In the http array I can define the proxy server settings, method which can be post or get, and the content.

    I use file_get_contents to retrieve the web page instead of cURL, which is more powerful.

    To test it,

    $url=”http://biz.thestar.com.my/marketwatch/main.asp”;
    $data=array(‘bns’=>’2′,
    ‘clp’=>’1′,
    ‘klseViewDate’=>’1/30/2008′
    );
    $content = http_post_content($url,$data);
    $htmlDoc = new DomDocument();
    @$htmlDoc->loadHTML($content);
    $xPath = new DOMXPath($htmlDoc);
    $counters = $xPath->evaluate(‘//table[@id="Table2"]
    /tr/td[2]/center/table/tr/td/span
    [@class="text"]/table/tr/td/a’);
    for ($i = 0; $i length; $i++){
    print($counters->item($i)->nodeValue.’‘);
    }
    I pass in the URL and the post data, then I use XQuery to retrieve the information that I want.

    Note the HTML may not be well-formed, so I suppress the warning by prefixing a @ in front.

    One of the catch here is the correct XQuery/XPath to be used. You can always find out using Solvent, which is a very good FireFox plug-in for web page scraping.
    quote]

  • if you guys are looking for good tools to get back links, i found some good ones at this website here

    http://www.webthangs.com

Leave a Reply

You must login to post a comment.