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
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:fun@harbourpark.com">fun@harbourpark.com</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-->

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

$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) { }

Link to comment
Share on other sites

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();
?>

Link to comment
Share on other sites

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();
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.