Saturday, January 13, 2024

How to data scrap from other website

 Hello.

Today I am going to explain how to scrap data from other website. 

Web scraping requires careful consideration and adherence to ethical and legal standards. Before proceeding, ensure that you have the right to access and scrape the content, according to the website's terms of service.



Suppose we have one website and we want to copy some of its div data.


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Scraping Example</title>
<script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
</head>
<body>
<div id="output"></div>
<script>
$(document).ready(function() {
// Set the URL of the target website
var url = 'https://www.portotheme.com/wordpress/porto_landing/';
// Make an AJAX request to fetch the HTML content
$.get(url, function(data) {
// Create a jQuery object from the HTML data
var $html = $(data);
// Use jQuery to select the desired div
var $targetDiv = $html.find('.row.sample-item-list.sample-item-list-loaded.sort-destination');
// Process each item within the div
$targetDiv.find('div.col-sm-6.col-lg-4.col-xxl-3').each(function() {
// Extract data for each item
var title = $(this).find('h3').text();
var link = $(this).find('a').attr('href');
// Get the inline style of the span.sample-item element
var inlineStyle = $(this).find('span.sample-item').attr('style') || '';
// Retrieve data-oi value
var dataOiValue = $(this).find('.sample-item').data('oi');
var fileName = dataOiValue.split('/').pop();
// Display the data in the output div
$('#output').append('<p>Title: ' + title + ', Link: ' + link + ' , filename:'+fileName+'</p>');
});
});
});
</script>
</body>
</html>

No comments:

Post a Comment