<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Development - Devhour</title>
	<atom:link href="https://www.devhour.net/tag/development/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.devhour.net</link>
	<description>Taking time to write about development</description>
	<lastBuildDate>Tue, 12 Mar 2024 10:01:14 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.4.3</generator>

<image>
	<url>https://www.devhour.net/wp-content/uploads/2024/03/cropped-devhourlogo-32x32.png</url>
	<title>Development - Devhour</title>
	<link>https://www.devhour.net</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Reading a websites contents using PHP and XPath</title>
		<link>https://www.devhour.net/reading-a-websites-contents-using-php-and-xpath/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=reading-a-websites-contents-using-php-and-xpath</link>
					<comments>https://www.devhour.net/reading-a-websites-contents-using-php-and-xpath/#respond</comments>
		
		<dc:creator><![CDATA[Jamie]]></dc:creator>
		<pubDate>Mon, 16 Sep 2013 12:32:00 +0000</pubDate>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Dev Hour]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[XPath]]></category>
		<guid isPermaLink="false">https://www.devhour.net/?p=32</guid>

					<description><![CDATA[<p>Following on from my previous post Scraping data with PHP and cURL today I’m going to show you about reading a websites contents using PHP and XPath. One of the biggest problems I have faced when dealing with a scraped web page (read: block of HTML) is how to go about getting specific text elements out of it. Several years [&#8230;]</p>
<p>The post <a href="https://www.devhour.net/reading-a-websites-contents-using-php-and-xpath/">Reading a websites contents using PHP and XPath</a> first appeared on <a href="https://www.devhour.net">Devhour</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>Following on from my previous post <a href="https://www.devhour.net/filling-out-forms-with-php-and-curl/">Scraping data with PHP and cURL</a> today I’m going to show you about reading a websites contents using <a href="https://php.net/" target="_blank" rel="noreferrer noopener">PHP</a> and <a href="https://www.w3schools.com/xpath/xpath_syntax.asp" target="_blank" rel="noreferrer noopener">XPath</a>.</p>



<p>One of the biggest problems I have faced when dealing with a scraped web page (read: block of HTML) is how to go about getting specific text elements out of it. Several years ago I would’ve gone with regex. This was a hassle to me and I’ve still yet to get my head around how it correctly works. These days though, I have discovered a useful little thing called XPath.</p>



<p>Basically, how it works is, you put the HTML in a DOMDocument and then you can use XPath to navigate through elements and attributes of the document getting the data of them in the process.</p>



<h2 class="wp-block-heading">Using PHP and XPath</h2>



<p>Now if we were to consider the following HTML:</p>



<div class="wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#575279;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#faf4ed"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="&lt;html&gt;   
  &lt;head&gt;
    &lt;title&gt;Test&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;div id=&quot;mainDiv&quot;&gt;
      &lt;div id=&quot;leftDiv&quot;&gt;
        &lt;p class=&quot;bodyText&quot;&gt;This is left&lt;/p&gt;
        &lt;img src=&quot;images/test.jpg&quot; /&gt;
      &lt;/div&gt;
      &lt;div id=&quot;rightDiv&quot;&gt;
        &lt;p class=&quot;bodyText&quot;&gt;This is right&lt;/p&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div id=&quot;footer&quot;&gt;
      &lt;p class=&quot;smallText&quot;&gt;This is the footer&lt;/p&gt;
    &lt;/div&gt;
  &lt;/body&gt;
&lt;/html&gt;" style="color:#575279;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #9893A5">&lt;</span><span style="color: #56949F">html</span><span style="color: #9893A5">&gt;</span><span style="color: #575279">   </span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">head</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">    </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">title</span><span style="color: #9893A5">&gt;</span><span style="color: #575279">Test</span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">title</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">head</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">body</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">    </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">div</span><span style="color: #575279"> </span><span style="color: #907AA9; font-style: italic">id</span><span style="color: #797593">=</span><span style="color: #EA9D34">&quot;mainDiv&quot;</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">      </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">div</span><span style="color: #575279"> </span><span style="color: #907AA9; font-style: italic">id</span><span style="color: #797593">=</span><span style="color: #EA9D34">&quot;leftDiv&quot;</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">        </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">p</span><span style="color: #575279"> </span><span style="color: #907AA9; font-style: italic">class</span><span style="color: #797593">=</span><span style="color: #EA9D34">&quot;bodyText&quot;</span><span style="color: #9893A5">&gt;</span><span style="color: #575279">This is left</span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">p</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">        </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">img</span><span style="color: #575279"> </span><span style="color: #907AA9; font-style: italic">src</span><span style="color: #797593">=</span><span style="color: #EA9D34">&quot;images/test.jpg&quot;</span><span style="color: #575279"> </span><span style="color: #9893A5">/&gt;</span></span>
<span class="line"><span style="color: #575279">      </span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">div</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">      </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">div</span><span style="color: #575279"> </span><span style="color: #907AA9; font-style: italic">id</span><span style="color: #797593">=</span><span style="color: #EA9D34">&quot;rightDiv&quot;</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">        </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">p</span><span style="color: #575279"> </span><span style="color: #907AA9; font-style: italic">class</span><span style="color: #797593">=</span><span style="color: #EA9D34">&quot;bodyText&quot;</span><span style="color: #9893A5">&gt;</span><span style="color: #575279">This is right</span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">p</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">      </span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">div</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">    </span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">div</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">    </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">div</span><span style="color: #575279"> </span><span style="color: #907AA9; font-style: italic">id</span><span style="color: #797593">=</span><span style="color: #EA9D34">&quot;footer&quot;</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">      </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">p</span><span style="color: #575279"> </span><span style="color: #907AA9; font-style: italic">class</span><span style="color: #797593">=</span><span style="color: #EA9D34">&quot;smallText&quot;</span><span style="color: #9893A5">&gt;</span><span style="color: #575279">This is the footer</span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">p</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">    </span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">div</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">body</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">html</span><span style="color: #9893A5">&gt;</span></span></code></pre><span style="display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#faf4ed;color:#625c88;font-size:12px;line-height:1;position:relative">HTML</span></div>



<p>And now, lets say I wanted to get the text of the rightDiv. This is how we would go about it:</p>



<div class="wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#575279;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#faf4ed"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="&lt;?php
  // $content is the content you scraped (via curl for example)
  $dom = new DOMDocument();
  @$dom-&gt;loadHTML($content)
  $xpath = new DOMXPath($dom);
  $rightDivText = $xpath-&gt;query(&quot;//html/body/div[@id='mainDiv']/div[@id='rightDiv']/p/text()&quot;); 
  
  // Returns: This is right
  echo $rightDivText-&gt;item(0)-&gt;nodeValue;
?&gt;" style="color:#575279;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #286983">&lt;?php</span></span>
<span class="line"><span style="color: #797593">  </span><span style="color: #797593; font-style: italic">//</span><span style="color: #9893A5; font-style: italic"> $content is the content you scraped (via curl for example)</span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">dom</span><span style="color: #575279"> </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #286983">new</span><span style="color: #575279"> </span><span style="color: #56949F">DOMDocument</span><span style="color: #797593">();</span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #286983">@</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">dom</span><span style="color: #286983">-&gt;</span><span style="color: #D7827E">loadHTML</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">content</span><span style="color: #797593">)</span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">xpath</span><span style="color: #575279"> </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #286983">new</span><span style="color: #575279"> </span><span style="color: #56949F">DOMXPath</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">dom</span><span style="color: #797593">);</span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">rightDivText</span><span style="color: #575279"> </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">xpath</span><span style="color: #286983">-&gt;</span><span style="color: #D7827E">query</span><span style="color: #797593">(</span><span style="color: #EA9D34">&quot;//html/body/div[@id=&#39;mainDiv&#39;]/div[@id=&#39;rightDiv&#39;]/p/text()&quot;</span><span style="color: #797593">);</span><span style="color: #575279"> </span></span>
<span class="line"><span style="color: #575279">  </span></span>
<span class="line"><span style="color: #797593">  </span><span style="color: #797593; font-style: italic">//</span><span style="color: #9893A5; font-style: italic"> Returns: This is right</span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #B4637A; font-style: italic">echo</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">rightDivText</span><span style="color: #286983">-&gt;</span><span style="color: #D7827E">item</span><span style="color: #797593">(</span><span style="color: #D7827E">0</span><span style="color: #797593">)</span><span style="color: #286983">-&gt;</span><span style="color: #575279; font-style: italic">nodeValue</span><span style="color: #797593">;</span></span>
<span class="line"><span style="color: #286983">?&gt;</span></span></code></pre><span style="display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#faf4ed;color:#625c88;font-size:12px;line-height:1;position:relative">PHP</span></div>



<p>Simply, it will load the $content (HTML) into a DOMDocument and then using XPath it looks for the for the div with the id of ‘mainDiv’ and then inside of ‘mainDiv’ it looks for the div with the id of ‘rightDiv’. Finally it then gets the text of the ‘p’ element.</p>



<p>Now that is one of the more straight forward scenarios and is made easier by the fact that most of the different elements have custom IDs like ‘mainDiv’ or ‘rightDiv’. If we were to look at the same HTML block again but this time without IDs, this is how it would look.</p>



<div class="wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#575279;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#faf4ed"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;Test&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;div&gt;
      &lt;div&gt;
        &lt;p class=&quot;bodyText&quot;&gt;This is left&lt;/p&gt;
        &lt;img src=&quot;images/test.jpg&quot; /&gt;
      &lt;/div&gt;

      &lt;div&gt;
        &lt;p class=&quot;bodyText&quot;&gt;This is right&lt;/p&gt;
      &lt;/div&gt;
    &lt;/div&gt;

    &lt;div id=&quot;footer&quot;&gt;
      &lt;p class=&quot;smallText&quot;&gt;This is the footer&lt;/p&gt;
    &lt;/div&gt;
  &lt;/body&gt;
&lt;/html&gt;
" style="color:#575279;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #9893A5">&lt;</span><span style="color: #56949F">html</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">head</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">    </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">title</span><span style="color: #9893A5">&gt;</span><span style="color: #575279">Test</span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">title</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">head</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">body</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">    </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">div</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">      </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">div</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">        </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">p</span><span style="color: #575279"> </span><span style="color: #907AA9; font-style: italic">class</span><span style="color: #797593">=</span><span style="color: #EA9D34">&quot;bodyText&quot;</span><span style="color: #9893A5">&gt;</span><span style="color: #575279">This is left</span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">p</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">        </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">img</span><span style="color: #575279"> </span><span style="color: #907AA9; font-style: italic">src</span><span style="color: #797593">=</span><span style="color: #EA9D34">&quot;images/test.jpg&quot;</span><span style="color: #575279"> </span><span style="color: #9893A5">/&gt;</span></span>
<span class="line"><span style="color: #575279">      </span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">div</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #575279">      </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">div</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">        </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">p</span><span style="color: #575279"> </span><span style="color: #907AA9; font-style: italic">class</span><span style="color: #797593">=</span><span style="color: #EA9D34">&quot;bodyText&quot;</span><span style="color: #9893A5">&gt;</span><span style="color: #575279">This is right</span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">p</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">      </span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">div</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">    </span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">div</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"></span>
<span class="line"><span style="color: #575279">    </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">div</span><span style="color: #575279"> </span><span style="color: #907AA9; font-style: italic">id</span><span style="color: #797593">=</span><span style="color: #EA9D34">&quot;footer&quot;</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">      </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">p</span><span style="color: #575279"> </span><span style="color: #907AA9; font-style: italic">class</span><span style="color: #797593">=</span><span style="color: #EA9D34">&quot;smallText&quot;</span><span style="color: #9893A5">&gt;</span><span style="color: #575279">This is the footer</span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">p</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">    </span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">div</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">body</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">html</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"></span></code></pre><span style="display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#faf4ed;color:#625c88;font-size:12px;line-height:1;position:relative">HTML</span></div>



<p>This time lets get the src of the image in the former leftDiv.</p>



<div class="wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#575279;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#faf4ed"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="&lt;?php
// $content is the content you scraped (via curl for example)
$dom = new DOMDocument();  @$dom-&gt;loadHTML($content)
$xpath = new DOMXPath($dom);
$imgSrc = $xpath-&gt;query(&quot;//html/body/div[1]/div[1]/img/@src&quot;);

// Returns: images/test.jpg
echo $imgSrc-&gt;item(0)-&gt;nodeValue;
?&gt;
" style="color:#575279;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #286983">&lt;?php</span></span>
<span class="line"><span style="color: #797593; font-style: italic">//</span><span style="color: #9893A5; font-style: italic"> $content is the content you scraped (via curl for example)</span></span>
<span class="line"><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">dom</span><span style="color: #575279"> </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #286983">new</span><span style="color: #575279"> </span><span style="color: #56949F">DOMDocument</span><span style="color: #797593">();</span><span style="color: #286983">  @</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">dom</span><span style="color: #286983">-&gt;</span><span style="color: #D7827E">loadHTML</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">content</span><span style="color: #797593">)</span></span>
<span class="line"><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">xpath</span><span style="color: #575279"> </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #286983">new</span><span style="color: #575279"> </span><span style="color: #56949F">DOMXPath</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">dom</span><span style="color: #797593">);</span></span>
<span class="line"><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">imgSrc</span><span style="color: #575279"> </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">xpath</span><span style="color: #286983">-&gt;</span><span style="color: #D7827E">query</span><span style="color: #797593">(</span><span style="color: #EA9D34">&quot;//html/body/div[1]/div[1]/img/@src&quot;</span><span style="color: #797593">);</span></span>
<span class="line"></span>
<span class="line"><span style="color: #797593; font-style: italic">//</span><span style="color: #9893A5; font-style: italic"> Returns: images/test.jpg</span></span>
<span class="line"><span style="color: #B4637A; font-style: italic">echo</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">imgSrc</span><span style="color: #286983">-&gt;</span><span style="color: #D7827E">item</span><span style="color: #797593">(</span><span style="color: #D7827E">0</span><span style="color: #797593">)</span><span style="color: #286983">-&gt;</span><span style="color: #575279; font-style: italic">nodeValue</span><span style="color: #797593">;</span></span>
<span class="line"><span style="color: #286983">?&gt;</span></span>
<span class="line"></span></code></pre><span style="display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#faf4ed;color:#625c88;font-size:12px;line-height:1;position:relative">PHP</span></div>



<p>As you can see, the difference this time is that instead of using the divs ID, we used the elements number. Inside the body, it looks for the first div, and then inside of that it again looks for the first child div before returning the src of the img element.</p>



<p>Again though, this is probably another pretty simple solution but definitely gives you an idea of how XPath works and some of the ways it can be used. The next scenario is basically the one I came face to face with, and that scenario is one in which we have multiple divs with the same name. Consider this html:</p>



<div class="wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#575279;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#faf4ed"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;Test&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;div id=&quot;container&quot;&gt;
      &lt;div id=&quot;person&quot;&gt;
        &lt;p&gt;Bob&lt;/p&gt;
        &lt;p&gt;25&lt;/p&gt;
        &lt;p&gt;180lbs&lt;/p&gt;
        &lt;img src=&quot;images/p1.jpg&quot; /&gt;
      &lt;/div&gt;
      &lt;div id=&quot;person&quot;&gt;
        &lt;p&gt;Stacy&lt;/p&gt;
        &lt;p&gt;26&lt;/p&gt;
        &lt;p&gt;187lbs&lt;/p&gt;
        &lt;img src=&quot;images/p2.jpg&quot; /&gt;
      &lt;/div&gt;
      &lt;div id=&quot;person&quot;&gt;
        &lt;p&gt;John&lt;/p&gt;
        &lt;p&gt;21&lt;/p&gt;
        &lt;p&gt;255lbs&lt;/p&gt;
        &lt;img src=&quot;images/p3.jpg&quot; /&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div id=&quot;footer&quot;&gt;&lt;p class=&quot;smallText&quot;&gt;This is the footer&lt;/p&gt;&lt;/div&gt;
  &lt;/body&gt;
&lt;/html&gt;
" style="color:#575279;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #9893A5">&lt;</span><span style="color: #56949F">html</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">head</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">    </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">title</span><span style="color: #9893A5">&gt;</span><span style="color: #575279">Test</span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">title</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">head</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">body</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">    </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">div</span><span style="color: #575279"> </span><span style="color: #907AA9; font-style: italic">id</span><span style="color: #797593">=</span><span style="color: #EA9D34">&quot;container&quot;</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">      </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">div</span><span style="color: #575279"> </span><span style="color: #907AA9; font-style: italic">id</span><span style="color: #797593">=</span><span style="color: #EA9D34">&quot;person&quot;</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">        </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">p</span><span style="color: #9893A5">&gt;</span><span style="color: #575279">Bob</span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">p</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">        </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">p</span><span style="color: #9893A5">&gt;</span><span style="color: #575279">25</span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">p</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">        </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">p</span><span style="color: #9893A5">&gt;</span><span style="color: #575279">180lbs</span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">p</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">        </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">img</span><span style="color: #575279"> </span><span style="color: #907AA9; font-style: italic">src</span><span style="color: #797593">=</span><span style="color: #EA9D34">&quot;images/p1.jpg&quot;</span><span style="color: #575279"> </span><span style="color: #9893A5">/&gt;</span></span>
<span class="line"><span style="color: #575279">      </span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">div</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">      </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">div</span><span style="color: #575279"> </span><span style="color: #907AA9; font-style: italic">id</span><span style="color: #797593">=</span><span style="color: #EA9D34">&quot;person&quot;</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">        </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">p</span><span style="color: #9893A5">&gt;</span><span style="color: #575279">Stacy</span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">p</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">        </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">p</span><span style="color: #9893A5">&gt;</span><span style="color: #575279">26</span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">p</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">        </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">p</span><span style="color: #9893A5">&gt;</span><span style="color: #575279">187lbs</span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">p</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">        </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">img</span><span style="color: #575279"> </span><span style="color: #907AA9; font-style: italic">src</span><span style="color: #797593">=</span><span style="color: #EA9D34">&quot;images/p2.jpg&quot;</span><span style="color: #575279"> </span><span style="color: #9893A5">/&gt;</span></span>
<span class="line"><span style="color: #575279">      </span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">div</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">      </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">div</span><span style="color: #575279"> </span><span style="color: #907AA9; font-style: italic">id</span><span style="color: #797593">=</span><span style="color: #EA9D34">&quot;person&quot;</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">        </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">p</span><span style="color: #9893A5">&gt;</span><span style="color: #575279">John</span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">p</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">        </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">p</span><span style="color: #9893A5">&gt;</span><span style="color: #575279">21</span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">p</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">        </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">p</span><span style="color: #9893A5">&gt;</span><span style="color: #575279">255lbs</span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">p</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">        </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">img</span><span style="color: #575279"> </span><span style="color: #907AA9; font-style: italic">src</span><span style="color: #797593">=</span><span style="color: #EA9D34">&quot;images/p3.jpg&quot;</span><span style="color: #575279"> </span><span style="color: #9893A5">/&gt;</span></span>
<span class="line"><span style="color: #575279">      </span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">div</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">    </span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">div</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">    </span><span style="color: #9893A5">&lt;</span><span style="color: #56949F">div</span><span style="color: #575279"> </span><span style="color: #907AA9; font-style: italic">id</span><span style="color: #797593">=</span><span style="color: #EA9D34">&quot;footer&quot;</span><span style="color: #9893A5">&gt;&lt;</span><span style="color: #56949F">p</span><span style="color: #575279"> </span><span style="color: #907AA9; font-style: italic">class</span><span style="color: #797593">=</span><span style="color: #EA9D34">&quot;smallText&quot;</span><span style="color: #9893A5">&gt;</span><span style="color: #575279">This is the footer</span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">p</span><span style="color: #9893A5">&gt;&lt;/</span><span style="color: #56949F">div</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #575279">  </span><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">body</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"><span style="color: #9893A5">&lt;/</span><span style="color: #56949F">html</span><span style="color: #9893A5">&gt;</span></span>
<span class="line"></span></code></pre><span style="display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#faf4ed;color:#625c88;font-size:12px;line-height:1;position:relative">HTML</span></div>



<p>So here we have a webpage that displays a bunch of people with information such as their name, age, weight, and picture. Now as someone who is interested in getting the information of all of these people, I need to scrape the website and then using XPath I can loop through each person and get it.</p>



<p>To do this, we get everything inside the ‘container’ div and then loop through each individual ‘person’ div. Then using XPath on the ‘person’ div we can use the above examples to get the information we want. Finally we store all the results in an array.</p>



<div class="wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#575279;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#faf4ed"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code=" &lt;?php
$dom = new DOMDocument();
@$dom-&gt;loadHTML($content);
$tempDom   = new DOMDocument();
$xpath     = new DOMXPath($dom);
$container = $xpath-&gt;query(&quot;//div[@id='container']&quot;);
foreach ($container as $item) {
    $tempDom-&gt;appendChild($tempDom-&gt;importNode($item, true));
}
$tempDom-&gt;saveHTML();
$peopleXpath = new DOMXPath($tempDom);
$peopleDiv   = $peopleXpath-&gt;query(&quot;div[@id='person']&quot;);
$results     = array();
foreach ($peopleDiv as $people) {
    $newDom = new DOMDocument;
    $newDom-&gt;appendChild($newDom-&gt;importNode($people, true));
    $personXpath = new DOMXPath($newDom);
    $name        = trim($personXpath-&gt;query(&quot;p[1]/text()&quot;)-&gt;item(0)-&gt;nodeValue);
    $age         = trim($resultXpath-&gt;query(&quot;p[2]/text()&quot;)-&gt;item(0)-&gt;nodeValue);
    $weight      = trim($resultXpath-&gt;query(&quot;p[3]/text()&quot;)-&gt;item(0)-&gt;nodeValue);
    $image       = trim($resultXpath-&gt;query(&quot;img[1]/@src&quot;)-&gt;item(0)-&gt;nodeValue);
    $results[]   = array(
        'name' =&gt; $name,
        'age' =&gt; $age,
        'weight' =&gt; $weight,
        'image' =&gt; $image
    );
}
?&gt; " style="color:#575279;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #575279"> </span><span style="color: #286983">&lt;?php</span></span>
<span class="line"><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">dom</span><span style="color: #575279"> </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #286983">new</span><span style="color: #575279"> </span><span style="color: #56949F">DOMDocument</span><span style="color: #797593">();</span></span>
<span class="line"><span style="color: #286983">@</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">dom</span><span style="color: #286983">-&gt;</span><span style="color: #D7827E">loadHTML</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">content</span><span style="color: #797593">);</span></span>
<span class="line"><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">tempDom</span><span style="color: #575279">   </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #286983">new</span><span style="color: #575279"> </span><span style="color: #56949F">DOMDocument</span><span style="color: #797593">();</span></span>
<span class="line"><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">xpath</span><span style="color: #575279">     </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #286983">new</span><span style="color: #575279"> </span><span style="color: #56949F">DOMXPath</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">dom</span><span style="color: #797593">);</span></span>
<span class="line"><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">container</span><span style="color: #575279"> </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">xpath</span><span style="color: #286983">-&gt;</span><span style="color: #D7827E">query</span><span style="color: #797593">(</span><span style="color: #EA9D34">&quot;//div[@id=&#39;container&#39;]&quot;</span><span style="color: #797593">);</span></span>
<span class="line"><span style="color: #286983">foreach</span><span style="color: #575279"> </span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">container</span><span style="color: #575279"> </span><span style="color: #286983">as</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">item</span><span style="color: #797593">)</span><span style="color: #575279"> </span><span style="color: #797593">{</span></span>
<span class="line"><span style="color: #575279">    </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">tempDom</span><span style="color: #286983">-&gt;</span><span style="color: #D7827E">appendChild</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">tempDom</span><span style="color: #286983">-&gt;</span><span style="color: #D7827E">importNode</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">item</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #D7827E">true</span><span style="color: #797593">));</span></span>
<span class="line"><span style="color: #797593">}</span></span>
<span class="line"><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">tempDom</span><span style="color: #286983">-&gt;</span><span style="color: #D7827E">saveHTML</span><span style="color: #797593">();</span></span>
<span class="line"><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">peopleXpath</span><span style="color: #575279"> </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #286983">new</span><span style="color: #575279"> </span><span style="color: #56949F">DOMXPath</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">tempDom</span><span style="color: #797593">);</span></span>
<span class="line"><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">peopleDiv</span><span style="color: #575279">   </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">peopleXpath</span><span style="color: #286983">-&gt;</span><span style="color: #D7827E">query</span><span style="color: #797593">(</span><span style="color: #EA9D34">&quot;div[@id=&#39;person&#39;]&quot;</span><span style="color: #797593">);</span></span>
<span class="line"><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">results</span><span style="color: #575279">     </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #B4637A; font-style: italic">array</span><span style="color: #797593">();</span></span>
<span class="line"><span style="color: #286983">foreach</span><span style="color: #575279"> </span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">peopleDiv</span><span style="color: #575279"> </span><span style="color: #286983">as</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">people</span><span style="color: #797593">)</span><span style="color: #575279"> </span><span style="color: #797593">{</span></span>
<span class="line"><span style="color: #575279">    </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">newDom</span><span style="color: #575279"> </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #286983">new</span><span style="color: #575279"> </span><span style="color: #56949F">DOMDocument</span><span style="color: #797593">;</span></span>
<span class="line"><span style="color: #575279">    </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">newDom</span><span style="color: #286983">-&gt;</span><span style="color: #D7827E">appendChild</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">newDom</span><span style="color: #286983">-&gt;</span><span style="color: #D7827E">importNode</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">people</span><span style="color: #797593">,</span><span style="color: #575279"> </span><span style="color: #D7827E">true</span><span style="color: #797593">));</span></span>
<span class="line"><span style="color: #575279">    </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">personXpath</span><span style="color: #575279"> </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #286983">new</span><span style="color: #575279"> </span><span style="color: #56949F">DOMXPath</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">newDom</span><span style="color: #797593">);</span></span>
<span class="line"><span style="color: #575279">    </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">name</span><span style="color: #575279">        </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #B4637A; font-style: italic">trim</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">personXpath</span><span style="color: #286983">-&gt;</span><span style="color: #D7827E">query</span><span style="color: #797593">(</span><span style="color: #EA9D34">&quot;p[1]/text()&quot;</span><span style="color: #797593">)</span><span style="color: #286983">-&gt;</span><span style="color: #D7827E">item</span><span style="color: #797593">(</span><span style="color: #D7827E">0</span><span style="color: #797593">)</span><span style="color: #286983">-&gt;</span><span style="color: #575279; font-style: italic">nodeValue</span><span style="color: #797593">);</span></span>
<span class="line"><span style="color: #575279">    </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">age</span><span style="color: #575279">         </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #B4637A; font-style: italic">trim</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">resultXpath</span><span style="color: #286983">-&gt;</span><span style="color: #D7827E">query</span><span style="color: #797593">(</span><span style="color: #EA9D34">&quot;p[2]/text()&quot;</span><span style="color: #797593">)</span><span style="color: #286983">-&gt;</span><span style="color: #D7827E">item</span><span style="color: #797593">(</span><span style="color: #D7827E">0</span><span style="color: #797593">)</span><span style="color: #286983">-&gt;</span><span style="color: #575279; font-style: italic">nodeValue</span><span style="color: #797593">);</span></span>
<span class="line"><span style="color: #575279">    </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">weight</span><span style="color: #575279">      </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #B4637A; font-style: italic">trim</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">resultXpath</span><span style="color: #286983">-&gt;</span><span style="color: #D7827E">query</span><span style="color: #797593">(</span><span style="color: #EA9D34">&quot;p[3]/text()&quot;</span><span style="color: #797593">)</span><span style="color: #286983">-&gt;</span><span style="color: #D7827E">item</span><span style="color: #797593">(</span><span style="color: #D7827E">0</span><span style="color: #797593">)</span><span style="color: #286983">-&gt;</span><span style="color: #575279; font-style: italic">nodeValue</span><span style="color: #797593">);</span></span>
<span class="line"><span style="color: #575279">    </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">image</span><span style="color: #575279">       </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #B4637A; font-style: italic">trim</span><span style="color: #797593">(</span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">resultXpath</span><span style="color: #286983">-&gt;</span><span style="color: #D7827E">query</span><span style="color: #797593">(</span><span style="color: #EA9D34">&quot;img[1]/@src&quot;</span><span style="color: #797593">)</span><span style="color: #286983">-&gt;</span><span style="color: #D7827E">item</span><span style="color: #797593">(</span><span style="color: #D7827E">0</span><span style="color: #797593">)</span><span style="color: #286983">-&gt;</span><span style="color: #575279; font-style: italic">nodeValue</span><span style="color: #797593">);</span></span>
<span class="line"><span style="color: #575279">    </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">results</span><span style="color: #797593">[]</span><span style="color: #575279">   </span><span style="color: #286983">=</span><span style="color: #575279"> </span><span style="color: #B4637A; font-style: italic">array</span><span style="color: #797593">(</span></span>
<span class="line"><span style="color: #575279">        </span><span style="color: #EA9D34">&#39;name&#39;</span><span style="color: #575279"> </span><span style="color: #286983">=&gt;</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">name</span><span style="color: #797593">,</span></span>
<span class="line"><span style="color: #575279">        </span><span style="color: #EA9D34">&#39;age&#39;</span><span style="color: #575279"> </span><span style="color: #286983">=&gt;</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">age</span><span style="color: #797593">,</span></span>
<span class="line"><span style="color: #575279">        </span><span style="color: #EA9D34">&#39;weight&#39;</span><span style="color: #575279"> </span><span style="color: #286983">=&gt;</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">weight</span><span style="color: #797593">,</span></span>
<span class="line"><span style="color: #575279">        </span><span style="color: #EA9D34">&#39;image&#39;</span><span style="color: #575279"> </span><span style="color: #286983">=&gt;</span><span style="color: #575279"> </span><span style="color: #797593; font-style: italic">$</span><span style="color: #575279; font-style: italic">image</span></span>
<span class="line"><span style="color: #575279">    </span><span style="color: #797593">);</span></span>
<span class="line"><span style="color: #797593">}</span></span>
<span class="line"><span style="color: #286983">?&gt;</span><span style="color: #575279"> </span></span></code></pre><span style="display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#faf4ed;color:#625c88;font-size:12px;line-height:1;position:relative">PHP</span></div>



<p>There you have it. If you run a print_r on the results array you should have yourself a bunch values containing the information of Bob, Stacy, and John.</p>



<p>Now a totally awesome cheat for this solution is to use the Google Chrome <a href="https://chrome.google.com/webstore/detail/xpath-helper/hgimnogjllphhhkhlmebbmlgjoejdpjl?hl=en" target="_blank" rel="noreferrer noopener">XPath Helper</a> extension. All you need to do is hold down the shift key as you hover over webpage elements. This in turn will return the XPath query which is a HUGE help!</p>



<p>As always feel free to comment and or improve on the above. Also, don’t forget to follow me <a href="https://twitter.com/JAGracie" target="_blank" rel="noreferrer noopener">@JAGracie</a>.</p><p>The post <a href="https://www.devhour.net/reading-a-websites-contents-using-php-and-xpath/">Reading a websites contents using PHP and XPath</a> first appeared on <a href="https://www.devhour.net">Devhour</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://www.devhour.net/reading-a-websites-contents-using-php-and-xpath/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Raspberry Pi and Motion (Weather App Pt 2)</title>
		<link>https://www.devhour.net/raspberry-pi-and-motion-weather-app-pt-2/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=raspberry-pi-and-motion-weather-app-pt-2</link>
					<comments>https://www.devhour.net/raspberry-pi-and-motion-weather-app-pt-2/#respond</comments>
		
		<dc:creator><![CDATA[Jamie]]></dc:creator>
		<pubDate>Mon, 06 May 2013 12:12:00 +0000</pubDate>
				<category><![CDATA[Raspberry Pi]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Motion]]></category>
		<guid isPermaLink="false">https://www.devhour.net/?p=17</guid>

					<description><![CDATA[<p>Part 2 in this series is going to show you how to setup Raspberry Pi and Motion in order to snap a picture every 10 minutes or so. If you haven’t already read Part 1 in this series to check out how to set up a Raspberry Pi temperature sensor with the DS18B20 sensor. Having setup the [&#8230;]</p>
<p>The post <a href="https://www.devhour.net/raspberry-pi-and-motion-weather-app-pt-2/">Raspberry Pi and Motion (Weather App Pt 2)</a> first appeared on <a href="https://www.devhour.net">Devhour</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>Part 2 in this series is going to show you how to setup Raspberry Pi and Motion in order to snap a picture every 10 minutes or so.</p>



<p>If you haven’t already read Part 1 in this series to check out how to set up a <a href="https://www.devhour.net/raspberry-pi-temperature-weather-app/">Raspberry Pi temperature sensor</a> with the DS18B20 sensor.</p>



<p>Having setup the temperature sensor and tested that it can read the temperature correctly, I can now move on to setting up our camera to capture an image of the weather every 10 minutes.</p>



<p>Fortunately I did have a spare web cam lying around but you could get away with spending a couple of dollars and getting a fairly cheap web camera that’ll do the trick.</p>



<p>Now to do this I am going to use a pretty cool piece of software called <a href="https://www.lavrsen.dk/foswiki/bin/view/Motion">Motion</a>. Motion is a piece of software which uses video4linux device to detect motion and can be used to take both videos and pictures. It also has a pretty powerful trigger action settings which allows you to run shell commands after/before a certain event in order to perform extra actions (eg Send an email or upload the video/picture). I will make use of these in the next part of this series in order to grab the current temperature after the image has been taken.</p>



<p>One of the cool things people have used Motion for is to make a basic home security system. Have a webcam pointed at the driveway or main entrance to your house and then have motion take a video or picture of anyone who approaches. Upload the video to a remote location and send you an email letting you know you have a visitor. Works quite well and for ~$50 is a cheap solution.</p>



<p>For now though I am going simpler. Tell Motion to take a picture of whatever the web camera is pointing at every 10 minutes and move the picture to our images folder (later used for the website I make). In my case I will be pointing my web cam out the window in order to see the weather.</p>



<h2 class="wp-block-heading">Setup Raspberry Pi and Motion</h2>



<p>Firstly, install and setup motion on the Raspberry Pi.</p>



<div class="wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#575279;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#faf4ed"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="# Update and upgrade the Raspberry Pi
sudo apt-get update
sudo apt-get upgrade

# Install motion
sudo apt-get install motion" style="color:#575279;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #797593; font-style: italic">#</span><span style="color: #9893A5; font-style: italic"> Update and upgrade the Raspberry Pi</span></span>
<span class="line"><span style="color: #D7827E">sudo</span><span style="color: #575279"> </span><span style="color: #EA9D34">apt-get</span><span style="color: #575279"> </span><span style="color: #EA9D34">update</span></span>
<span class="line"><span style="color: #D7827E">sudo</span><span style="color: #575279"> </span><span style="color: #EA9D34">apt-get</span><span style="color: #575279"> </span><span style="color: #EA9D34">upgrade</span></span>
<span class="line"></span>
<span class="line"><span style="color: #797593; font-style: italic">#</span><span style="color: #9893A5; font-style: italic"> Install motion</span></span>
<span class="line"><span style="color: #D7827E">sudo</span><span style="color: #575279"> </span><span style="color: #EA9D34">apt-get</span><span style="color: #575279"> </span><span style="color: #EA9D34">install</span><span style="color: #575279"> </span><span style="color: #EA9D34">motion</span></span></code></pre><span style="display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#faf4ed;color:#625c88;font-size:12px;line-height:1;position:relative">Bash</span></div>



<p>Then edit the config file.</p>



<div class="wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#575279;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#faf4ed"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="# Tell the service to start in daemon mode
sudo nano /etc/default/motion
start_motion_daemon=yes

# Make these changes to the motion config file
sudo nano /etc/motion/motion.conf
daemon on
snapshot_interval 600
output_normal off
ffmpeg_cap_new off
target_dir /home/pi/motion/images" style="color:#575279;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #797593; font-style: italic">#</span><span style="color: #9893A5; font-style: italic"> Tell the service to start in daemon mode</span></span>
<span class="line"><span style="color: #575279">sudo nano /etc/default/motion</span></span>
<span class="line"><span style="color: #286983">start_motion_daemon</span><span style="color: #797593">=</span><span style="color: #575279">yes</span></span>
<span class="line"></span>
<span class="line"><span style="color: #797593; font-style: italic">#</span><span style="color: #9893A5; font-style: italic"> Make these changes to the motion config file</span></span>
<span class="line"><span style="color: #575279">sudo nano /etc/motion/motion.conf</span></span>
<span class="line"><span style="color: #575279">daemon on</span></span>
<span class="line"><span style="color: #575279">snapshot_interval 600</span></span>
<span class="line"><span style="color: #575279">output_normal off</span></span>
<span class="line"><span style="color: #575279">ffmpeg_cap_new off</span></span>
<span class="line"><span style="color: #575279">target_dir /home/pi/motion/images</span></span></code></pre><span style="display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#faf4ed;color:#625c88;font-size:12px;line-height:1;position:relative">INI</span></div>



<p>The above changes made to the config are only a handful of the options you will see inside the file. The general idea of the changes I made are to turn off video recording and to tell motion to take a snapshot every 10 minutes and to then save the file to the images directory. Feel free to play around but these settings will get me doing what I need to do.</p>



<h2 class="wp-block-heading">Start Motion on the Raspberry Pi</h2>



<p>You probably want to start motion in debug mode the first time around just to make sure everything is as it should be.</p>



<div class="wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#575279;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#faf4ed"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="sudo motion -n" style="color:#575279;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #D7827E">sudo</span><span style="color: #575279"> </span><span style="color: #EA9D34">motion</span><span style="color: #575279"> </span><span style="color: #286983">-n</span></span></code></pre><span style="display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#faf4ed;color:#625c88;font-size:12px;line-height:1;position:relative">Bash</span></div>



<p>This will display output to the screen and every 10 minutes you should see snapshots getting saved to the output directory.</p>



<p>Check the directory and download the images to make sure you are getting the desired output and if all is as expected you can now start the motion service.</p>



<div class="wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled cbp-has-line-numbers" data-code-block-pro-font-family="Code-Pro-JetBrains-Mono" style="font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#575279;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)"><span style="display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#faf4ed"><svg xmlns="http://www.w3.org/2000/svg" width="54" height="14" viewBox="0 0 54 14"><g fill="none" fill-rule="evenodd" transform="translate(1 1)"><circle cx="6" cy="6" r="6" fill="#FF5F56" stroke="#E0443E" stroke-width=".5"></circle><circle cx="26" cy="6" r="6" fill="#FFBD2E" stroke="#DEA123" stroke-width=".5"></circle><circle cx="46" cy="6" r="6" fill="#27C93F" stroke="#1AAB29" stroke-width=".5"></circle></g></svg></span><span role="button" tabindex="0" data-code="sudo service motion start" style="color:#575279;display:none" aria-label="Copy" class="code-block-pro-copy-button"><svg xmlns="http://www.w3.org/2000/svg" style="width:24px;height:24px" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path class="with-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"></path><path class="without-check" stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"></path></svg></span><pre class="shiki rose-pine-dawn" style="background-color: #faf4ed" tabindex="0"><code><span class="line"><span style="color: #D7827E">sudo</span><span style="color: #575279"> </span><span style="color: #EA9D34">service</span><span style="color: #575279"> </span><span style="color: #EA9D34">motion</span><span style="color: #575279"> </span><span style="color: #EA9D34">start</span></span></code></pre><span style="display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#faf4ed;color:#625c88;font-size:12px;line-height:1;position:relative">Bash</span></div>



<p>That’s it! Raspberry Pi and Motion is now set up. Pretty easy and straight forward. Like I said though, this is one of the more basic uses of motion and it can be used to do a lot more. Have a play around and let me know what you come up with. I’m always interested!</p>



<p>The next part of this tutorial series I will be showing you how to write a python script which will be executed using the motion action trigger.</p>



<p>So stay tuned and as always you can follow me on twitter <a href="https://twitter.com/JAGracie">@JAGracie</a></p><p>The post <a href="https://www.devhour.net/raspberry-pi-and-motion-weather-app-pt-2/">Raspberry Pi and Motion (Weather App Pt 2)</a> first appeared on <a href="https://www.devhour.net">Devhour</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://www.devhour.net/raspberry-pi-and-motion-weather-app-pt-2/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>The Dev Hour begins</title>
		<link>https://www.devhour.net/the-dev-hour-begins/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=the-dev-hour-begins</link>
					<comments>https://www.devhour.net/the-dev-hour-begins/#respond</comments>
		
		<dc:creator><![CDATA[Jamie]]></dc:creator>
		<pubDate>Wed, 10 Apr 2013 08:11:00 +0000</pubDate>
				<category><![CDATA[Other]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Devhour]]></category>
		<guid isPermaLink="false">https://www.devhour.net/?p=11</guid>

					<description><![CDATA[<p>The first few years of my working life has seen me fall slightly behind in the world of web development so I’m taking the time to get a blog together to write down my findings and any interesting little tidbits (/titbits) I find along the way. Bare with me as I try to catch up [&#8230;]</p>
<p>The post <a href="https://www.devhour.net/the-dev-hour-begins/">The Dev Hour begins</a> first appeared on <a href="https://www.devhour.net">Devhour</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>The first few years of my working life has seen me fall slightly behind in the world of web development so I’m taking the time to get a blog together to write down my findings and any interesting little tidbits (/titbits) I find along the way.</p>



<p>Bare with me as I try to catch up (may find it impossible as this part of the world moves at a rate of knots). I’ll probably be talking about pieces of code that have been around for some time now. Feel free to help out with a comment or 2 to help get me up to speed, as I’ll&nbsp;appreciate&nbsp;any help I come across.</p><p>The post <a href="https://www.devhour.net/the-dev-hour-begins/">The Dev Hour begins</a> first appeared on <a href="https://www.devhour.net">Devhour</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://www.devhour.net/the-dev-hour-begins/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
