xpath giving wrong output in simple html dom - xpath

I'm trying to scrape every line on this url
http://www.gosugamers.net/counterstrike/news/archive
I've used xpath-helper to create following path:
//div[class='content']/table[#class='simple gamelist medium']/tbody/tr
this should print every line in the tbody however when i try this in simple html dom, it returns the thead with the title, date and comment. How come it does not return the tbody instead as it does in xpath helper?
include('simple_html_dom.php');
function getHTML($url,$timeout)
{
$ch = curl_init($url); // initialize curl with given url
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]); // set useragent
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // write the response to a variable
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // follow redirects if any
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); // max. seconds to execute
curl_setopt($ch, CURLOPT_FAILONERROR, 1); // stop when it encounters an error
return #curl_exec($ch);
}
$html = str_get_html(getHTML("http://www.gosugamers.net/counterstrike/news/archive",10));
$table = $html->find("//div[class='content']/table[#class='simple gamelist medium']/tbody/tr",0);
echo $table;

Update:
The simplehtmldom library doesn't appear to support positional predicates in XPath. To get a specific row, you need to pass a 0-based index as the second parameter to find().
To get the first non-header row (the second table row):
$table = $html->find("//div[class='content']/table[#class='simple gamelist medium']/tbody/tr", 1);
working phpfiddle
You XPath expression is selecting each `tr` element. If you want the whole `tbody` element, remove `/tr` from the end of the expression.
If you only want the table cells (`td`), add `/td[1]`.
If you only want the titles, add `/td[1]/a/string()`.

Related

Is there a way of parsing information from smarty?

I have three files, a weather.php file which contacts an API and returns weather to my weather.tpl file. However I want to take the contents of the weather.tpl file (which only contains the api response) and post it to another page, any ideas about how I can go about doing this? I'm using smarty version 3.1.33.
Here are my files.
// Weather.php
<?php
$ch = curl_init();
$location = 'MyLocation';
$apikey = 'MyApiKey';
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, 'http://api.openweathermap.org/data/2.5/weather?q='.$location.'&appid='.$apikey);
$output = curl_exec($ch);
curl_close($ch);//Free up system resource
$output = json_decode($output, true);
echo "The weather in ".$location." for MusicWorks Festival will be ".$output['main']['temp'];
?>
//weather.tpl
{block name="weather"}
{/block}
//account.tpl (where I want the contents to end up)
<div>
{block name="weather"}The weather: {/block}
</div>
I have used smarty like this to display both pages.
{extends file="layouts/main.tpl"}
{block name="body"}

Full youtube palylist using curl ( only half list is dispalying using normal curl )

click to check how the list is loading
i am using the following code to curl youtube but it is showing only top 100 videos in playlist how can i show full playlist videos. There are 180 videos in playlist
( i know about API but i want to use this without youtube API)
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.youtube.com/playlist?list=PL1FO3JrU9Zy-A-q8s4FdzgzNVWsIcTx5q');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0");
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$contents = curl_exec ($ch);
echo $contents;
curl_close ($ch);
?>
Which procedure should i follow to get the full playlist ?
I have got my answer.
I need to curl the remaining videos by
Firstly Gather information like url, parameters and request type (post/get) from that ajax request.( by clicking on inspect and the click on network leaflet)
Generate the same request from your php/curl code and you got it.

Codeigniter Curl

I am new to CI. I am uploading file from a url(drop box file chooser) but now its uploading to the root directory. I want to know how I can give path to file upload inside the CURL functions.
below is the code .
$curl = curl_init($url);
$file = fopen(basename($url), 'wb');
$file_name =(basename($url));
curl_setopt($curl, CURLOPT_FILE, $file);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_exec($curl);
curl_close($curl);
fclose($file);
Thank you.
That is not possible to set file upload path on curl functon direct.
Please use of drop box api library in codeigniter link below.
drop box api :- https://github.com/jimdoescode/CodeIgniter-Dropbox-API-Library
and follow step.

My ajax request isn't working

I have an <ul id="keuze_lijst"> , an input field with id #sykje and an button with class .search .
Now when i press the button i would like to clear the UL and repopulate it with data, for this i currently got this .js file.
$(document).ready(function() {
$(".search").click(function(){
var searchValue = $("#sykje").val();
$("#keuze_lijst").empty();
$.ajax({
url: 'http://www.autive.nl/frysk/simulator/sim.php?action=getSongs&search='+searchValue,
data: "",
dataType: 'json',
success: function(rows) {
for(var i in rows){
var row = rows[i];
var id = row[1];
var titel = row[2];
var artiest = row[9];
$("#keuze_lijst").append("<li class='mag_droppen'>"+
"<div class='song_left'>"+
"<div class='titel'>"+titel+"</div>"+
"<div class='artiest'>"+artiest+"</div>"+
"</div><!-- .song_left -->"+
"</li>");
}
}
});
});
});
When i remove the ajax command and put something like $("#keuze_lijst").html("hello"); it works fine. But the ajax command isn't working. Though the var searchValue does his work. (ajax uses the correct url). And when i enter that url the page echoes an fine json with multiple rows.
But in my page the ajax script isn't adding the <li>.
What am i doing wrong?
edit: added an jsfiddle -> http://jsfiddle.net/TVvKb/1/
.html() totally replaces the HTML. So at the end, your "#keuze_list will contain </li>.
Just execute one html() command after you build your html into a string var or something.
From a quick glance, I can say that the problem might be with your use of the html() function. This actually replaces the entire html content.
You might want to try using append() or prepend().
Possible Problems:
You are running into a Same Origin Problem. Per default you can only make Ajax-Requests to your own domain. If you need to make cross-domain calls use JSONP or CORS.
Use the html() only once, and hand over your complete string, otherwise you will override your previous html all the time.
You are not landing in the success handler due to an error (e.g. invalid JSON).
Not sure, but I think if you insert a string in the .append() and other jQuery methods, it parses to (valid) HTML first. That means that unclosed tags are closed, making your HTML invalid.
$('<div />'); // parses to <div></div>
So, I assume that your DOM ends up like this this:
$('ul').append('<li>').append('foo').append('</li>'); // <ul><li></li>foo</li></ul>
Please, just format your string first. You don't want jQuery to parse every input.
var str = '<li>';
str += 'foo';
str += '</li>';
$('ul').html(str);
For cross-domain AJAX requests (without JSONP):
proxy.php
header('Content-Type: application/json');
if(empty($_GET['search'])) exit;
$url = 'http://www.autive.nl/frysk/simulator/sim.php?action=getSongs&search=' . $_GET['search'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_exec($ch);
curl_close($ch);
Javascript
$.getJSON({
url: 'proxy.php&search='+searchValue,
success: callback
});

Load text from specific external DIV using AJAX?

I'm trying to load up the estimated world population from http://www.census.gov/ipc/www/popclockworld.html using AJAX, and so far, failing miserably.
There's a DIV with the ID "worldnumber" on that page which contains the estimated population, so that's the only text I want to grab from the page.
Here's what I've tried:
$(document).ready(function(){
$("#population").load('http://www.census.gov/ipc/www/popclockworld.html #worldnumber *');
});
What you are trying to do is known as a cross-domain request. This is not a feature that browsers normally allow (security feature). Some ways to get around this limitation are described here: The jQuery Cross-Domain Ajax Guide.
you can try something like this:
$.get('http://www.census.gov/ipc/www/popclockworld.html', function(content) {
$("#population").html($('#worldnumber',$(content)));
});
Yeah, it's security. You can't ajax in to pages that aren't from the same domain.
#R0MANARMY:
I couldn't seem to follow the directions given on that site you linked to, but I did figure out a solution... I created a PHP file with the following code:
//Run cURL call
$ch = curl_init('http://www.census.gov/main/www/rss/popclocks.xml');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
curl_close($ch);
//Set as new XML object
$doc = new SimpleXmlElement($data, LIBXML_NOCDATA);
function parseRSS($xml) {
$cnt = count($xml->channel->item);
for($i=0; $i<$cnt; $i++) {
$title = $xml->channel->item[$i]->title;
if ( preg_match("/world population estimate:\s([0-9,]+)\s/i", $title, $match) ) {
echo $match[1];
}
}
}
parseRSS($doc);
Then I called it with jQuery like so:
<div id="population"></div>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#population').load('getpop.php');
var refreshId = setInterval(function() {
$('#population').load('getpop.php');
}, 120000);
});
</script>
Just thought I'd post it here in case anyone else is looking to do something similar.

Resources