Jump to content

Complex - Getting An Element By Its Attribute Then Going Through Parent Nodes


ApocalypeX

Recommended Posts

So I am writing a script that gets information off of a website with stats on it.The stats change in order of frequency, so no id or tag is ever in the same place. Here is what a box of 1 stat is. There are multiple stat boxes.

<div class="weapon_container"><div id="ctl00_mainContent_rptWeaponsAll_ctl24_ctl00_liOnOver" onmouseover="java script:showObject('ctl00_mainContent_rptWeaponsAll_ctl24_ctl00_pnlWeaponDetails');" onmouseout="java script:hideObject('ctl00_mainContent_rptWeaponsAll_ctl24_ctl00_pnlWeaponDetails');"><div class="weapon"><img id="ctl00_mainContent_rptWeaponsAll_ctl24_ctl00_imgWeapon" src="/images/halo3stats/weapons/4e290a72-cd2b-4abf-8a98-8e851264d8b7.gif" style="border-width: 0px;"></div><div style="visibility: hidden;" id="ctl00_mainContent_rptWeaponsAll_ctl24_ctl00_pnlWeaponDetails" class="weapon_list_overlay">	<div class="top"><div class="message"><div class="title">Plasma Pistol - (Type-25 DEP)</div><div class="description">Fires directed energy bolts with a shield-depleting Overcharge function.</div></div><hr><div class="overlay_img"><img id="ctl00_mainContent_rptWeaponsAll_ctl24_ctl00_imgWeaponDetail" src="/images/halo3stats/weapons/2e382a07-99f6-47fc-b756-7dbb5287ddd5.gif" style="border-width: 0px;"></div><div class="total"><div class="description">Ranked</div><div class="number">1 (0.001)</div></div><div class="total"><div class="description">Social</div><div class="number">0 (0.000)</div></div><div class="total"><hr><div class="description">Total</div><div class="number">1 (0.001)</div></div></div></div></div><div id="ctl00_mainContent_rptWeaponsAll_ctl24_ctl00_pnlWeapon" class="weaponUse">	<div class="progressBars"><div><div id="ctl00_mainContent_rptWeaponsAll_ctl24_ctl00_pnlMeterTop" class="topPercent" style="width: 0px;">	</div><div class="percentText">0.01%</div></div><div class="clear"></div><div><div id="ctl00_mainContent_rptWeaponsAll_ctl24_ctl00_pnlMeterBot" class="botPercent_s" style="width: 0px;">	</div><div class="percentText_s"></div></div></div><div class="weaponNumbers"><div class="top">1 (0.001)</div><div class="bot">0 (0.000)</div></div></div></div>    

Now I am using this code to get the page

$doc = new DOMDocument();$doc->loadHTMLFile("http://www.bungie.net/stats/halo3/careerstats.aspx?player=ApocalypeX");

I need a way that I can somehow search for a IMG tag with the SRC attribute of /images/halo3stats/weapons/2e382a07-99f6-47fc-b756-7dbb5287ddd5.gif and then go up its parentNodes twice. To store that as an element so I can then sweep through to get the data I need.I know...

Link to comment
Share on other sites

I don't like embedding so many strings in my code, but it works, and if you need to change it you can at least see what needs to be changed.

<?php	$doc = new DOMDocument;	$doc->loadHTMLFile("http://www.bungie.net/stats/halo3/careerstats.aspx?player=ApocalypeX");	$items = $doc->getElementsByTagName('img');	foreach( $items as $image ) {		$path = $image->getAttribute('src');		if ($path == "/images/halo3stats/weapons/2e382a07-99f6-47fc-b756-7dbb5287ddd5.gif") {			break;		}	}	$mynode = $image->parentNode->parentNode;?>

Link to comment
Share on other sites

I don't like embedding so many strings in my code, but it works, and if you need to change it you can at least see what needs to be changed.
<?php	$doc = new DOMDocument;	$doc->loadHTMLFile("http://www.bungie.net/stats/halo3/careerstats.aspx?player=ApocalypeX");	$items = $doc->getElementsByTagName('img');	foreach( $items as $image ) {		$path = $image->getAttribute('src');		if ($path == "/images/halo3stats/weapons/2e382a07-99f6-47fc-b756-7dbb5287ddd5.gif") {			break;		}	}	$mynode = $image->parentNode->parentNode;?>

Thank you so much good sir. :)
Link to comment
Share on other sites

Does that code work? I'd think $image is not available outside of the foreach... Unless I'm mistaken, it should be more like:

<?php	$doc = new DOMDocument;	$doc->loadHTMLFile("http://www.bungie.net/stats/halo3/careerstats.aspx?player=ApocalypeX");	$items = $doc->getElementsByTagName('img');	$mynode;	foreach( $items as $image ) {		$path = $image->getAttribute('src');		if ($path == "/images/halo3stats/weapons/2e382a07-99f6-47fc-b756-7dbb5287ddd5.gif") {			$mynode = $image->parentNode->parentNode;			break;		}	}}?>

(though "$mynode;" can surely be omitted...)

Link to comment
Share on other sites

Does that code work? I'd think $image is not available outside of the foreach
It works on my system, anyway, and I don't think it's configured in some strange way. Adding this:
echo $mynode->nodeValue;

Gets me this:

Plasma Pistol - (Type-25 DEP)Fires directed energy bolts with a shield-depleting Overcharge function. Ranked1 (0.001) Social0 (0.000) Total1 (0.001)
Presumably I'd simply get an error if it didn't work.Your way would be a good hedge against different scoping rules that might apply in different versions. There's certainly no reason not to add another variable (unless we were going to run this thing 100K times, maybe).EDIT: It turns out that the output I quoted above is a concatenation of all the descendant text nodes. When I tested it yesterday, what I echoed was the value of the node's "class" attribute. I'd forgotten. I made a point of testing the process, though, because I'd never used an HTML DOM Object before. Only XML. And never from a remote source. So I wasn't entirely sure what would happen. I was quite pleased that the code worked right out of the box.
Link to comment
Share on other sites

It works on my system, anyway, and I don't think it's configured in some strange way. Adding this:
echo $mynode->nodeValue;

Gets me this:Presumably I'd simply get an error if it didn't work.Your way would be a good hedge against different scoping rules that might apply in different versions. There's certainly no reason not to add another variable (unless we were going to run this thing 100K times, maybe).EDIT: It turns out that the output I quoted above is a concatenation of all the descendant text nodes. When I tested it yesterday, what I echoed was the value of the node's "class" attribute. I'd forgotten. I made a point of testing the process, though, because I'd never used an HTML DOM Object before. Only XML. And never from a remote source. So I wasn't entirely sure what would happen. I was quite pleased that the code worked right out of the box.

It works perfectly dont worry, I cant thank you enough this bit of code I can use for numerous of my projects.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...