Jump to content

Database query


rofl90

Recommended Posts

Hi guys I have a list of sites that have been generated by a script, eg www.someweb.com/a-a-a-d102289.html

 

how could I crawl each of them, take some information from a specific div for instance ATTRACTION_REVIEW

 

and make a query to insert the contents of the div into a database

Link to comment
https://forums.phpfreaks.com/topic/109767-database-query/
Share on other sites

<div id="ATTRACTION_REVIEW" class="listing">
<div class="details">
<div class="information textual">
<div class="description">
<b>Traveler Description:</b> <div class="toggle">
<span class="onHide">Attractions include favorites such as the Log Flume, Dodgems, Adventure Golf, Pan for Gold or brave the Horror Hotel. Located next to the sandy...
<span class="show">more &#xbb;</span> </span>
<span class="onShow">Attractions include favorites such as the Log Flume, Dodgems, Adventure Golf, Pan for Gold or brave the Horror Hotel. Located next to the sandy beaches, marina and working harbour at Littlehampton in West Sussex. It has all you need for a great family day out. New for 2008 - Indoor Cannon Ball Play Zone & Caterpillar Roller Coaster. Book online for family and group discounts.
<span class="hide">&#xab; less</span> </span>

</div>
</div>
<div class="description addDesc">
Familiar with Harbour Park? Write your own description and <a href="/Travel-g504226-d1005323/Littlehampton:United-Kingdom:Harbour.Park.html" rel="nofollow">share what you know</a> with other travelers.<br/>
</div>
</div><!--/ information.textual-->
<div class="information textual">
<div class="type"><b>Attraction type:</b> Amusement/theme park</div>
</div><!--/ information.textual-->
<div class="information bulleted">

<ul class="arrows">
<li><a target="_blank" href="http://www.harbourpark.com/">http://www.harbourpark.com/</a></li>
<li><a href="mailto:[email protected]">[email protected]</a></li>
</ul>
</div><!--/ information.bulleted-->
<div class="information textual adr">
<div>
<b>Address:</b> <span class="street-address">Seafront</span>
<span class="locality"> Littlehampton BN17 5LL</span>
<br/><span class="country-name">England</span> </div>

<div><b>Tel:</b> <span class="tel">01903 721200</span></div>
<div><b>Fax:</b> <span class="fax">01903 716663</span></div>
</div><!--/ information.textual-->
</div><!--/ details-->
</div><!--/ ATTRACTION_REVIEW.listing-->

You're luck that comment is there (the one that says that ATTRACTION_REVIEW.listing is over), otherwise you'd be screwed.  Here:

 

$file = file_get_contents("http://something.com/something.html");

$match = array();

preg_match('~<div id="ATTRACTION_REVIEW" class="listing">(.+?)</div><!--/ ATTRACTION_REVIEW.listing-->~i', $file, $match);

 

Then, $match[1] will have the content in that DIV.

Thanks - this is my current code -

<?php
ob_start();
$CONF = array();
$CONF["DATABASE"] 				= 'x';
$CONF["USERNAME"] 				= 'x';
$CONF["PASSWORD"] 				= 'x';
$CONF["HOST"] 					= 'x';
mysql_connect($CONF["HOST"], $CONF["USERNAME"], $CONF["PASSWORD"]);
mysql_select_db($CONF["DATABASE"]);
$first = 100000;
while($first < 100001) {
$fileName = "http://www.x.com/x-g--d" . $first . ".html";
$file = file_get_contents($fileName);
$match = array();
if(preg_match('~<div id="ATTRACTION_REVIEW" class="listing">(.+?)</div><!--/ ATTRACTION_REVIEW.listing-->~i', $file, $match)) {
	if(mysql_query("INSERT INTO pages (linkName, theText) VALUES('$fileName', '$match[1]')")) {
		echo "Success\n\n";
		$first++;
	}
	else {
		echo "Failure\n\n";
	}
}
}
ob_end_flush();
?>

 

It just continuously loads.

$first must not be getting incremented because this is failing:

 

mysql_query("INSERT INTO pages (linkName, theText) VALUES('$fileName', '$match[1]')")

 

Instead of using it in the if statement, do this:

 

$result = mysql_query("INSERT INTO pages (linkName, theText) VALUES('$fileName', '$match[1]')") OR die(mysql_error()); //important

if ($result) { }

Unfortunately still a continual load.

 

Heres code, minus the mysql stuff:

mysql_connect($CONF["HOST"], $CONF["USERNAME"], $CONF["PASSWORD"]);
mysql_select_db($CONF["DATABASE"]);
$first = 100000;
while($first < 100001) {
$fileName = "http://www.x.com/x-g--d" . $first . ".html";
$file = file_get_contents($fileName);
$match = array();
if(preg_match('~<div id="ATTRACTION_REVIEW" class="listing">(.+?)</div><!--/ ATTRACTION_REVIEW.listing-->~i', $file, $match)) {
	$result = mysql_query("INSERT INTO pages (linkName, theText) VALUES('$fileName', '$match[1]')") or die(mysql_error()); //important
	if($result) {
		echo "Success\n\n";
		$first++;
	}
	else {
		echo "Failure\n\n";
	}
}
}
ob_end_flush();
?>

Still continually loading:

 

code:

 

$first = 100000;
while($first < 100001) {
$fileName = "http://www.x.com/x-g--d" . $first . ".html";
$file = file_get_contents($fileName);
$match = array();
if(preg_match('~<div id="ATTRACTION_REVIEW" class="listing">(.+?)</div><!--/ ATTRACTION_REVIEW.listing-->~i', $file, $match)) {
	$result = mysql_query("INSERT INTO pages (linkName, theText) VALUES('$fileName', '$match[1]')") or die(mysql_error()); //important
	if($result) {
		echo "Success\n\n";
		$first++;
	}
	else {
		echo "Failure\n\n";
	}
}
else {
	echo "Error: preg_match part did not work";
}
}
ob_end_flush();
?>

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.