Scraping website in php with javascript code - php

I need to scrape this site,
here is link
makemytrip.
Can any of you guide me or give some hint how to scrape it in php?

Follow the methods:
the page to scarpe
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head> <title>This is the Title</title>
<meta name="description" content="Meta Description" />
<meta name="keywords" content="Meta Keywords" />
</head>
<body>
<ul>
<li>Link 1</li>
<li>Link 2</li>
<li>Link 3</li>
<li>Link 4</li>
<li>Link 5</li>
</ul>
</div>
</body>
scraping method
<?php
$file_string = file_get_contents('page_to_scrape');
preg_match('/<title>(.*)<\/title>/i', $file_string, $title);
$title_out = $title[1];
preg_match('/<meta name="keywords" content="(.*)" \/> /i', $file_string, $keywords);
$keywords_out = $keywords[1];
preg_match('/<meta name="description" content="(.*)" \/> /i', $file_string, $description);
$description_out = $description[1];
preg_match_all('/<li><a href="(.*)">(.*)<\/a><\/li>/i', $file_string, $links);
?>
<p><strong>Title:</strong> <?php echo $title_out; ?></p>
<p><strong>Keywords:</strong> <?php echo $keywords_out; ?></p>
<p><strong>Description:</strong> <?php echo $description_out; ?></p>
<p><strong>Links:</strong> <em>(Name - Link)</em><br />
<?php
echo '<ol>';
for($i = 0; $i < count($links[1]); $i++) {
echo '<li>' . $links[2][$i] . ' - ' . $links[1][$i] . '</li>';
}
echo '</ol>';
?>
</p>

Related

Change navigation bar if user is logged in

I want the "Login/Sign up" button in the navigation bar to change to "My Account" after the user logs in. I have a problem as my index.html page does not display the navigation bar.
index.html page:
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>Index Page</title>
</head>
<body>
<?php include("navigation.php"); ?>
</body>
</html>
navigation.php
<?php
include("check.php");
?>
<?php
if ($loginst == 1){ ?>
<div id="nav">
<ul >
<li class="navbar-left">Page 2</li>
<li class="navbar-left">Page 3</li>
<li class="navbar-right">My Account</li>
<li class="navbar-right"><a href="logout.php">Sign Out</a</li>
</ul>
</div>
<?php } else { ?>
<div id="nav">
<ul >
<li class="navbar-left">Page 2</li>
<li class="navbar-left">Page 3</li>
<li class="navbar-right"><a href="login.php">Login</a</li>
</ul>
</div>
<?php } ?>
check.php
<?php
include('connection.php');
session_start();
$loginst = 0;
if ($_SESSION['username']){
$user_check = $_SESSION['username'];
$ses_sql = mysqli_query($db,"SELECT username FROM users WHERE username='$user_check' ");
$row=mysqli_fetch_array($ses_sql,MYSQLI_ASSOC);
$login_user=$row['username'];
if(!empty($login_user))
{
$loginst = 1;
}
}
?>
If I go to index.html then nav bar is not displayed. What am I doing wrong? Thanks.
Because your file format is html and does not support php tags :
<?php include("navigation.php"); ?>
rename index.html to index.php for support php include.

how to use css link in php code?

hi i'm doing my web programming h/w and got a problem
this is code :
<!DOCTYPE html>
<html>
<head>
<title>Music Viewer</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link href="http://www.cs.washington.edu/education/courses/cse190m/09sp/labs/3-music/viewer.css" type="text/css" rel="stylesheet" />
</head>
<body>
<div id="header">
<h1>190M Music Playlist Viewer</h1>
<h2>Search Through Your Playlists and Music</h2>
</div>
<ul>
<?php
$songs = glob("songs/*.mp3");
foreach ($songs as $songfile) {
$text = file_get_contents($songfile);
file_put_contents($songfile, strrev($text));
basename($songfile)
?>
<li><a href <?= "$songfile" ?>> <?= basename($songfile) ?></a> </li>
<?php
}
?>
<?php
$txt = glob("songs/*.txt");
foreach ($txt as $textfile) {
$textt = file_get_contents($textfile);
file_put_contents($textfile, strrev($textt));
basename($textfile)
?>
<li><a href <?= "$textfile" ?>> <?= basename($textfile) ?></a> </li>
<?php
}
?>
</ul>
</body>
</html>
this is about showing the song lists in the folder using glob
and i wanna apply same css style from html code which is
<link href="http://www.cs.washington.edu/education/courses/cse190m/09sp/labs/3-music/viewer.css" type="text/css" rel="stylesheet" />
how to do this ???

Session with included header

I am having trouble getting this if statement to work using the included php file. I know that you have to test if the $_SESSION['name'] is set but for some reason its not letting me use this function in php to test if I set the session for the user.
Here is my Index.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="css/mystyles.css"/>
<link href='http://fonts.googleapis.com/css?family=Comfortaa:700' rel='stylesheet' type='text/css'>
<title></title>
</head>
<body>
<div id="wrapper">
<?php include 'includes/header.php';?>
<?php include 'includes/footer.php';?>
</div>
</body>
</html>
Here is my updated header.php
<?php
echo "<header>
<div class='social'>
<ul>
<li><a href='#'><img src='images/Social Media/Facebook.png'/></a></li>
<li><a href='#'><img src='images/Social Media/Twitter-Bird.png'/></a></li>
<li><a href='#'><img src='images/Social Media/Google-Plus.png'/></a></li>
<li><a href='#'><img src='images/Social Media/Linkedin.png'/></a></li>
</ul>
</div>
<div class='users'>";
if(isset($_SESSION['user']){
echo "
<ul>
<li><a href='profile.php'>Profile</a></li>
<li><a href='logout.php'>Logout</a></li>
</ul>";
} else{
echo "
<ul>
<li><a href='signin.php'>Sign-In</a></li>
<li><a href='register.php'>Register</a></li>
</ul>
</div>";
}
echo "</div>
<h1><a href='index.php'>CollegeConnection</a></h1>
</header>";
?>
Now I am getting this error
Parse error: syntax error, unexpected '{' in C:\xampp\htdocs\CollegeConnection\includes\header.php on line 13
This never will work.
Better way:
$content = "<div class='users'>";
if(isset($_SESSION['user']){
$content .= "<ul>";
$content .= "<li><a href='profile.php'>Profile</a></li>";
$content .= "<li><a href='logout.php'>Logout</a></li>";
$content .= "</ul>";
} else {
$content .= "<ul>";
$content .= "<li><a href='signin.php'>Sign-In</a></li>";
$content .= "<li><a href='register.php'>Register</a></li>";
$content .= "</ul>";
}
$content .= "</div>";
echo $content;
What you want to do is probably :
if(isset($_SESSION['user']){
echo "
<ul>
<li><a href='profile.php'>Profile</a></li>
<li><a href='logout.php'>Logout</a></li>
</ul>";
} else{
echo "
<ul>
<li><a href='signin.php'>Sign-In</a></li>
<li><a href='register.php'>Register</a></li>
</ul>
</div>";
}

MySQL and PHP display error

Hello i m new to MySQL and PHP under training please tell me where i m wrong
My problem occurs in ad01 and ad03 they are echo Advertise Here but ad02 echo properly
<?php require_once('Connections/localhost.php'); ?>
<?php
mysql_select_db($database_localhost, $localhost);
$query_advtDisplay = "SELECT * FROM advt";
$advtDisplay = mysql_query($query_advtDisplay, $localhost) or die(mysql_error());
$row_advtDisplay = mysql_fetch_assoc($advtDisplay);
$totalRows_advtDisplay = mysql_num_rows($advtDisplay);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Download Links</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="wrapper">
<div class="ad01">
<?php if ($row_advtDisplay['advt-no']=='ad01')
{
echo $row_advtDisplay['advt-content'];
}
else{ echo "Advertise Here";}
?>
</div>
<div class="middlebox">
<div class="ad02">
<?php if ($row_advtDisplay['advt-no']=='ad02')
{
echo $row_advtDisplay['advt-content'];
}
else{ echo "Advertise Here";}
?>
</div>
<div class="linkbox">
<p>Download Links</p>
<ul>
<li>Link 01</li>
<li>Link 02</li>
<li>Link 03</li>
<li>Link 04</li>
<li>Link 05</li>
</ul>
<ul>
<li>Link 06</li>
<li>Link 07</li>
<li>Link 08</li>
<li>Link 09</li>
<li>Link 10</li>
</ul>
<ul>
<li>Link 11</li>
<li>Link 12</li>
<li>Link 13</li>
<li>Link 14</li>
<li>Link 15</li>
</ul>
</div>
<div class="passwordbox">
<p>RAR Password</p>
</div>
</div>
<div class="ad03"><?php if ($row_advtDisplay['advt-no']=='ad03')
{
echo $row_advtDisplay['advt-content'];
}
else{ echo "Advertise Here";}
?></div>
<div class="clear"></div>
</div>
</body>
</html>
<?php
mysql_free_result($advtDisplay);
?>
only ad02 shows properly not ad01 and ad03 No Problem with Table only Coding Problem sorry for bad english
mysql_fetch_assoc will only return one row ever... so you need to loop through to see all your data.
$just_one_row = mysql_fetch_assoc($advtDisplay);
// how to loop through all the rows
while ($row = mysql_fetch_assoc($advtDisplay)) {
echo $row["advt-no"];
echo $row["advt-content"];
}
// maybe do something like...
$mydata = array();
while ($row = mysql_fetch_assoc($advtDisplay)) {
$mydata[ $row['advt-no'] ] = $row['advt-content'];
}
<!-- and then -->
<div class="ad01">
<?php
if ( isset($mydata['ad01']) && !empty($mydata['ad01']) ) {
echo $mydata['ad01'];
} else { echo "Advertise Here"; } ?>
</div>
and I have to say it... you should really be using PDO or mysqli.

Approaches to implement macro definitions in html

I would be great doing things like
<define tag="myTag" options="3">
<h1> #1 </h1>
<ul>
<li> #2
<li> #3
</ul>
</define>
and then use it:
<myTag option="foo" option="bar" option="bean" />
I regard macros as
really big advantage.
A work-around is using a macro processor like m4, or using php to simulate the macros efect. Any other technique to consider?
Perhaps obvious, but the C preprocessor can do the job.
index._html
#define _em(a) <em> a </em>
#define _image(a, b) <img src="a" b/>
#define _list(a, b, c) <h1> a </h1> \
<ul> \
<li> b </li> \
<li> c </li> \
</ul>
<!-- ___________________________________________________ -->
<!doctype html>
<html>
#define _theTile The Bar Title
#include "head._html"
<body>
_list(foo, bar, bean)
This is really _em(great)
_image(media/cat.jpg, )
_image(media/dog.jpg, width="25%" height="10px")
</body>
</html>
Being head._html
<head>
<meta charset="utf-8"/>
<title> _theTile </title>
<!-- more stuff ... -->
</head>
Then,
cpp -P index._html > index.html
produces:
<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
<title> The Bar Title </title>
<!-- more stuff ... -->
</head>
<body>
<h1> foo </h1> <ul> <li> bar </li> <li> bean </li> </ul>
This is really <em> great </em>
<img src="media/cat.jpg" />
<img src="media/dog.jpg" width="25%" height="10px"/>
</body>
</html>
If you want to do it in the text-editor level, consider using Zen Coding.
In javascript
<!doctype html>
<html>
<script>
function em(a) {
var text = " <em> $a </em>".replace("$a", a);
document.write(text);
}
function image(a, b) {
var text = '<img src="$a" $b />'.replace("$a", a).replace("$b", b);
document.write( text );
}
function list(a, b, c) {
var text = '<h1> $a </h1> \
<ul> \
<li> $b </li> \
<li> $c </li> \
</ul>'
.replace("$a", a).replace("$b", b).replace("$c", c);
document.write (text);
}
</script>
<body>
<p>
<script> list("foo", "bar", "bean") </script>
<p> This is really <script> em("great") </script>
<p>
<script> image ("prosper.jpg", 'width="35%"') </script>
</body>
</html>
Pros: no prepocessing needed.
Cons: A bit annoying (always write <script> </script>). No direct way to include external html (afaik).
Now with php:
<!-- index.php -->
<?php
function list_($a, $b, $c) {
echo "
<h1> $a </h1>
<ul>
<li> $b </li>
<li> $c </li>
</ul>
";
}
function em($a) {
echo "<em> $a </em>";
}
function image($a, $b) {
echo "<img src=\"$a\" $b/>";
}
?>
<!doctype html>
<html>
<?php
$theTitle='The Bar Title';
include 'head.php';
?>
<body>
<? list_(foo, bar, bean) ?>
This is really <? em(great) ?>
<? image('media/cat.jpg', '' ) ?>
<? image('media/dog.jpg', 'width="25%" height="10px"') ?>
</body>
</html>
<head>
<meta charset="utf-8"/>
<title> <? echo "$theTitle"; ?> </title>
<!-- more stuff ... -->
</head>
Then
$ php index.php > index.html
gives
<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
<title> The Bar Title </title>
<!-- more stuff ... -->
</head>
<body>
<h1> foo </h1>
<ul>
<li> bar </li>
<li> bean </li>
</ul>
This is really <em> great </em>
<img src="media/cat.jpg" />
<img src="media/dog.jpg" width="25%" height="10px"/>
</body>
</html>
I've written a single-class, zero-installation macro system aimed straight at HTML coding. You'll find it here:
aa_macro.py

Categories