0% found this document useful (0 votes)
131 views5 pages

Sampah

The PHP code scrapes product data from various e-commerce sites like Tokopedia and Bukalapak by making API calls, parsing the HTML response, and extracting relevant details like product names and prices. It also includes a method to scrape data from a JSON API and display it in a formatted table on a web page.

Uploaded by

FeriYanto
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
131 views5 pages

Sampah

The PHP code scrapes product data from various e-commerce sites like Tokopedia and Bukalapak by making API calls, parsing the HTML response, and extracting relevant details like product names and prices. It also includes a method to scrape data from a JSON API and display it in a formatted table on a web page.

Uploaded by

FeriYanto
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

https://www.octoparse.

com/tutorial-7/scrape-product-data-from-tokopedia
http://www.jacobward.co.uk/using-php-to-scrape-javascript-jquery-json-websites/
http://alfathdirk.github.io/2018/12/12/Scraping-tokopedia-with-Luwak/
https://www.tokopedia.com/search?st=product&q=usb
https://blog.javan.co.id/teknik-dasar-web-scraping-aa7d7e223093

<?php
class NCRSilverScraper {

// Class constructor method


function __construct() {

$this->useragent = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-


US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3'; // Setting useragent of a popular
browser

$handle = fopen('cookie.txt', 'w') or exit('Unable to create or open


cookie.txt file.'."\n"); // Opening or creating cookie file
fclose($handle); // Closing cookie file
$this->cookie = 'cookie.txt'; // Setting a cookie file to store
cookie
$this->timeout = 30; // Setting connection timeout in seconds

$this->loginUrl = 'https://mystore.ncrsilver.com/app/Account/LogOn';

// User login method


public function login() {

// Login values to POST as array


$postValues = http_build_query(
array(
'username' => $emailAddress,
'password' => $password,
'RememberMe' => 'true',
'IsAjaxRequest' => 'false'
)
);

$request = $this->curlPostFields($this->loginUrl, $postValues); //


Making cURL POST request

$login = json_decode($request); // Decoding the JSON response

if ($login->success == 1) {
// Successful login
$message = 'Successful login.'; // Assigning successful message
echo $message;
} elseif ($login->success == 0) {
$message = $login->error; // Assigning login error message
returned by server
echo $message;
exit(); // Ending program
} else {
$message = 'Unknown login error.'; // Assigning unknown login
error message
echo $message;
exit(); // Ending program
}
}

// User logout method


public function logout() {
$request = $this-
>curlPostFields('https://mystore.ncrsilver.com/app/Account/LogOff?
CancelLogin=true&isAjaxRequest=true', null); // Logging out
}

// Method to search and scrape existing members details


public function scrapePersons($searchString = '') {

$searchUrl = 'https://mystore.ncrsilver.com/app/Customer/GetCustomers';

$postValues = array(
'PageRowCount' => 1000,
'RequestedPageNum' => 1,
'TotalRowCount' => -1,
'SearchArg' => $searchString,
'SortDirection' => 'ASC',
'SortColumn' => 'Name',
'page' => 1,
'start' => 0,
'limit' => 1000,
'sort' => '[{"property":"Name","direction":"ASC"}]',
'isAjaxRequest' => true,
);

$search = $this->curlPostFields($searchUrl, $postValues);

return $search;
}

// Method to make a POST request using form fields


public function curlPostFields($postUrl, $postValues) {
$_ch = curl_init(); // Initialising cURL session

// Setting cURL options


curl_setopt($_ch, CURLOPT_SSL_VERIFYPEER, FALSE); // Prevent cURL
from verifying SSL certificate
curl_setopt($_ch, CURLOPT_FAILONERROR, TRUE); // Script should fail
silently on error
curl_setopt($_ch, CURLOPT_COOKIESESSION, TRUE); // Use cookies
curl_setopt($_ch, CURLOPT_FOLLOWLOCATION, TRUE); // Follow Location:
headers
curl_setopt($_ch, CURLOPT_RETURNTRANSFER, TRUE); // Returning
transfer as a string
curl_setopt($_ch, CURLOPT_COOKIEFILE, $this->cookie); // Setting
cookiefile
curl_setopt($_ch, CURLOPT_COOKIEJAR, $this->cookie); // Setting
cookiejar
curl_setopt($_ch, CURLOPT_USERAGENT, $this->useragent); // Setting
useragent
curl_setopt($_ch, CURLOPT_URL, $postUrl); // Setting URL to POST to
curl_setopt($_ch, CURLOPT_CONNECTTIMEOUT, $this->timeout); //
Connection timeout
curl_setopt($_ch, CURLOPT_TIMEOUT, $this->timeout); // Request timeout
curl_setopt($_ch, CURLOPT_POST, TRUE); // Setting method as POST
curl_setopt($_ch, CURLOPT_POSTFIELDS, $postValues); // Setting POST
fields (array)

$results = curl_exec($_ch); // Executing cURL session


curl_close($_ch); // Closing cURL session

return $results;
}

// Class destructor method


function __destruct() {
// Empty
}
}

// Let's run this baby and scrape us some data!


$testScrape = new NCRSilverScraper(); // Instantiating new object

$testScrape->login(); // Logging into server

$data = json_decode($testScrape->scrapePersons()); // Scraping people records


print_r($data);

$testScrape->logout(); // Logging out

?>

<?php

$ku_user = �usernya�;

$ku_pass = �passwordnya�;

//jalankan curl

$output = shell_exec(�curl -u �.$ku_user.�:�.$ku_pass.� �https://m.bukalapak.com/�.


$ku_user.�/products?desktop_view=0&view_mode=grid&wap_view=1��);

//mulai

$pecah = explode(�<div class=\'product_highlight view_all_categories\�>�,$output);

$result = $pecah[1];

//sampe

$pecah2 = explode(�<div class=\'box\�>�,$pecah[1]);

$result = $pecah2[0];

print_r($result);

?>
<?php

$ku_user = �usernya�;

$ku_pass = �passwordnya�;

//jalankan curl

$output = shell_exec(�curl -u �.$ku_user.�:�.$ku_pass.� �https://m.bukalapak.com/�.


$ku_user.�/products?desktop_view=0&view_mode=grid&wap_view=1��);

//mulai

$pecah = explode(�<div class=\'product_highlight view_all_categories\�>�,$output);

$result = $pecah[1];

//sampe

$pecah2 = explode(�<div class=\'box\�>�,$pecah[1]);

$result = $pecah2[0];

print_r($result);

?>

<?php
$sumber = 'http://ppid.jakarta.go.id/json?
url=http://data.jakarta.go.id/dataset/06f19910-82c3-428f-9e13-
14d848486f69/resource/a7cc5803-9993-427b-a3df-9745a233b38d/download/Lomba-
bercerita-anak-TerbaikEdited.csv';
$konten = file_get_contents($sumber);
$data = json_decode($konten, true);

//echo $data[1]["nama_lokasi"];
echo "<h1 align='center'>Jumlah lomba anak bercerita terbaik jakarta ada
".count($data)." Siswa dan Siswi</h1>";
echo "<br/>";
?>

<!DOCTYPE html>
<html>
<head>
<title>Menampilkan data json</title>
<style>
table {
width: 100%;
}
table tr td {
padding: 1rem;
}
</style>
</head>
<body>
<table border="1">
<tr>
<th>No</th>
<th>Tahun</th>
<th>Jenis Lomba</th>
<th>Juara</th>
<th>Nama</th>
<th>Sekolah</th>
<th>ID</th>
</tr>
<?php
for($a=0; $a < count($data); $a++)
{
print "<tr>";
// penomeran otomatis
print "<td>".$a."</td>";
// menayangkan
print "<td>".$data[$a]['tahun']."</td>";
print "<td>".$data[$a]['jenis']."</td>";
print "<td>".$data[$a]['juara']."</td>";
print "<td>".$data[$a]['nama']."</td>";
print "<td>".$data[$a]['sekolah']."</td>";
print "<td>".$data[$a]['id']."</td>";
print "</tr>";
}
?>
</table>
</body>
</html>

http://ppid.jakarta.go.id/json?url=http://data.jakarta.go.id/dataset/06f19910-82c3-
428f-9e13-14d848486f69/resource/a7cc5803-9993-427b-a3df-
9745a233b38d/download/Lomba-bercerita-anak-TerbaikEdited.csv

You might also like