Page 1 of 2
Scrape <td> into textbox
Posted: Tue Sep 23, 2014 2:08 pm
by kiranindia1986
Hello to All
I am new to PHP. I am trying to scrape website data to my textboxes.
I need "Lähiaadress" and "Asustusüksus" value to textboxes
Here is the link
http://geoportaal.maaamet.ee/url/xgis-k ... 1:006:0812
When i checked the viewsource of the link, we can see its not a well formed table. Any suggestion.
Re: Scrape <td> into textbox
Posted: Tue Sep 23, 2014 2:21 pm
by Celauran
Link is a 404, for starters.
Re: Scrape <td> into textbox
Posted: Tue Sep 23, 2014 2:33 pm
by kiranindia1986
Re: Scrape <td> into textbox
Posted: Tue Sep 23, 2014 3:27 pm
by Celauran
It's not a table at all. It's a list and the markup appears to be well-formed. How are you scraping it?
Re: Scrape <td> into textbox
Posted: Tue Sep 23, 2014 3:34 pm
by kiranindia1986
No idea in PHP, i am vb.net developer, i have scraped in Vb.net, copied all contents to Datagrid, then using regex i removed <li> and copied to textbox.
Re: Scrape <td> into textbox
Posted: Tue Sep 23, 2014 3:47 pm
by kiranindia1986
I have written following code, but its not retrieving any output.
Code: Select all
function codeAddress1() {
$html = file_get_contents('http://geoportaal.maaamet.ee/url/xgis-ky.php?ky=79401:006:0812');
preg_match_all("(<li.*?>.*?</li>)", $html, $matches);
$days = $matches[1];
echo $days;
}
Re: Scrape <td> into textbox
Posted: Tue Sep 23, 2014 4:53 pm
by Celauran
Code: Select all
$ /usr/bin/php -a
Interactive shell
php > $html = file_get_contents('http://geoportaal.maaamet.ee/url/xgis-ky.php?ky=79401:006:0812');
php > preg_match_all("(<li.*?>.*?</li>)", $html, $matches);
php > print_r($matches);
Array
(
[0] => Array
(
[0] => <li><b>Maakond</b> : Tartu maakond</li>
[1] => <li><b>Omavalitsus</b> : Tartu vald</li>
[2] => <li><b>Asustusüksus</b> : Tila küla</li>
[3] => <li><b>Tunnus</b> : 79401:006:0812</li>
[4] => <li><b>Lähiaadress</b> : Pärna allee 10</li>
[5] => <li><b>Sihtotstarve 1</b> : Elamumaa 100%</li>
[6] => <li><b>Sihtotstarve 2</b> : -</li>
[7] => <li><b>Sihtotstarve 3</b> : -</li>
[8] => <li><b>Pindala</b> : 3230 m²</li>
[9] => <li><b>Haritav maa</b> : -</li>
[10] => <li><b>Rohumaa</b> : -</li>
[11] => <li><b>Metsamaa</b> : -</li>
[12] => <li><b>Õuemaa</b> : 3230 m²</li>
[13] => <li><b>Ehitiste alune maa</b> : -</li>
[14] => <li><b>Muu maa</b> : -</li>
[15] => <li><b>Veealune maa</b> : -</li>
[16] => <li><b>Registreerimise aeg</b> : 06.september 2005. a.</li>
[17] => <li><b>Muudatuse registreerimise aeg</b> : 04.november 2008. a.</li>
[18] => <li><b>Registriosa</b> : korteriomand</li>
[19] => <li><b>Kinnistuspiirkond / jaoskond</b> : Tartu Maakohtu Tartu maakonna kinnistusjaoskond</li>
[20] => <li><b>Mõõdistamise kuupäev</b> : 01.august 2005. a.</li>
[21] => <li><b>Mõõdistaja</b> : OÜ Kinnisvaraekspert Tartu</li>
[22] => <li><b>Mõõdistamisviis</b> : mõõdistatud, L-EST</li>
)
)
That not what you're seeing?
I don't know if $matches[1] is necessarily what you want, mind you.
Re: Scrape <td> into textbox
Posted: Tue Sep 23, 2014 5:14 pm
by kiranindia1986
Thanks for your reply
Below code works for me,
Code: Select all
$html = file_get_contents("http://geoportaal.maaamet.ee/url/xgis-ky.php?ky=79401:006:0812");
preg_match_all('(<li.*?>.*?</li>)', $html, $matches);
$one=$matches[0][0];
Above is for one line. How can we assign $ one to my textbox? That is remaining now.
Re: Scrape <td> into textbox
Posted: Tue Sep 23, 2014 7:36 pm
by Celauran
Have your function return the value, echo it into the text box. Where are you running into difficulty?
Re: Scrape <td> into textbox
Posted: Wed Sep 24, 2014 3:53 am
by kiranindia1986
I added the below code :
Code: Select all
<input id="t49" type="text" class="textbox" value="<?php echo (@$one);?>">
I am not getting any output.
Re: Scrape <td> into textbox
Posted: Wed Sep 24, 2014 7:06 am
by Celauran
Remove the @ as it's only purpose is to suppress errors, which is going to make your life harder. Where is $one defined? Did you assign your function's return value to it?
Re: Scrape <td> into textbox
Posted: Wed Sep 24, 2014 11:11 am
by kiranindia1986
Some where i made it work (thanks to Igor Gavryliv).
Now only one part remaining, on button click, i want to copy text box (InputField) value to my URL
Like :
http://geoportaal.maaamet.ee/url/xgis-ky.php?ky= + my textbox (InputField) value For Example (
79401:006:0812)
Below code is executing on Form load, is that possible to call this php on button click and later javascript code. and value to OutputField textbox.
Code: Select all
<?php
$html = file_get_contents("http://geoportaal.maaamet.ee/url/xgis-ky.php?ky=79401:006:0812");
preg_match_all('(<li.*?>.*?</li>)', $html, $matches);
$one=$matches[0][0];
?>
<script type="text/javascript">
function Assign() {
document.getElementById("OutputField").value = "<?=$one?>";
}
</script>
<input id="OutputField" type="text" style="width:200px"/>
<input id="InputField" type="text" style="width:200px"/>
<input type="button" value="Assign Value" onclick="Assign()"/>
Re: Scrape <td> into textbox
Posted: Wed Sep 24, 2014 1:36 pm
by Celauran
That function looks pretty brittle, with the hardcoded ID. What are you trying to do here?
Re: Scrape <td> into textbox
Posted: Wed Sep 24, 2014 1:54 pm
by kiranindia1986
I would like to pass value to php variable from textbox, then i need to merge that variable with website address:
So it will be something like : "
http://geoportaal.maaamet.ee/url/xgis-ky.php?ky=" + inputbox Value.
Re: Scrape <td> into textbox
Posted: Wed Sep 24, 2014 2:20 pm
by Celauran
So you're fetching a value from that site, parsing the HTML to pull out a value, to send right back to that site?