{"id":12,"date":"2020-03-03T15:41:32","date_gmt":"2020-03-03T10:11:32","guid":{"rendered":"https:\/\/ganofins.com\/blog\/?p=12"},"modified":"2020-05-08T22:34:44","modified_gmt":"2020-05-08T17:04:44","slug":"how-to-scrape-a-website-using-php","status":"publish","type":"post","link":"https:\/\/ganofins.com\/blog\/how-to-scrape-a-website-using-php\/","title":{"rendered":"How to Scrape a website using PHP?"},"content":{"rendered":"\n<p>Hey Guys,&nbsp;<\/p>\n\n\n\n<p>Today I will tell you how you can scrape a website using PHP language. To scrape a website using PHP you need to include&nbsp;<a href=\"http:\/\/bit.ly\/2PIXu1R\">simple_html_dom.php<\/a>&nbsp;file in your PHP file.<\/p>\n\n\n\n<p>This file contains predefined functions to parse the html website or to search through the tags of that site. Keep in mind Scraping a website without the site\u2019s permission can be considered as illegal.<\/p>\n\n\n\n<p><strong>*This post is just for Educational Purpose.<\/strong><\/p>\n\n\n\n<p>First choose the website and the data on it which you want to Scrape. Here I am taking the example of&nbsp;<a href=\"https:\/\/www.androidheadlines.com\/\">AndroidHeadlines.com<\/a>&nbsp;site. From it we are going to scrape the Latest Headlines.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*Pvbd5i2iGJSUo3C1yHqfvQ.png\"><img decoding=\"async\" src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*Pvbd5i2iGJSUo3C1yHqfvQ.png\" alt=\"\"\/><\/a><figcaption>AndroidHeadlines Homepage<\/figcaption><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Step 1&nbsp;: First you need to start the PHP&nbsp;tags\u2013<\/h4>\n\n\n\n<script src=\"https:\/\/gist.github.com\/Ganofins\/d5bb73a99e1e724dbbbaccf7a0798ae9.js\"><\/script>\n\n\n\n<p><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Step 2&nbsp;: Second include the&nbsp;<a href=\"http:\/\/bit.ly\/2PIXu1R\">simple_html_dom<\/a>&nbsp;file in your PHP code and place that file into the same folder&nbsp;\u2013<\/h4>\n\n\n\n<script src=\"https:\/\/gist.github.com\/Ganofins\/cfff2f1b32604f894da2702c3b469dea.js\"><\/script>\n\n\n\n<p><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Step 3&nbsp;: Now create a variable which will contain a method named as file_get_html (this method will create the Document Object Model for the URL provided by the user inside it\u2019s parenthesis) \u2013<\/h4>\n\n\n\n<script src=\"https:\/\/gist.github.com\/Ganofins\/3031a2f55fb51b84b9a1c0ff13562f5d.js\"><\/script>\n\n\n\n<p><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Step 4&nbsp;: Now by using the variable $html, we can find the site\u2019s tag. So let\u2019s find the tag which contains all the latest posts. For finding the tag inside the $html variable we will use find() function&nbsp;\u2013<\/h4>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*BRZkdiL_T97_QnEPQVy2iw.png\" alt=\"\"\/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/Ganofins\/ec8ac36059c63886296fa53474e3d5bd.js\"><\/script>\n\n\n\n<p><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Step 5&nbsp;: As we only want to scrape the title of the headline and there being multiple headlines, we need to create an array to store all these headlines \u2013<\/h4>\n\n\n\n<script src=\"https:\/\/gist.github.com\/Ganofins\/6e77a37f08079e2eba1f497575b59c65.js\"><\/script>\n\n\n\n<p><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Step 6&nbsp;: Now we are going to find the tag which contains the title of the headline. As you can see the span tag contains the title. So, just scrape it and don\u2019t write any index at the end. Now we can directly save it to our titles array&nbsp;\u2013<\/h4>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*kgJj80NHHMZVQeSRWThurA.png\" alt=\"\"\/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/Ganofins\/db38c2f2bc5da3f1aa21c00ebd072e1b.js\"><\/script>\n\n\n\n<h3 class=\"wp-block-heading\"><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Step 7&nbsp;: Now to print the array titles use foreach or any other loop&nbsp;\u2013<\/h4>\n\n\n\n<script src=\"https:\/\/gist.github.com\/Ganofins\/21d5ed1cd39679c4eeb21493a1d2256f.js\"><\/script>\n\n\n\n<p><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Step 8&nbsp;: Finally, You\u2019ll obtain the scraped data as output in the following manner&nbsp;\u2013<\/h4>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/cdn-images-1.medium.com\/max\/800\/1*Sr7Vt6KjBm1T3zu2f5CpRQ.png\" alt=\"\"\/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><\/h3>\n\n\n\n<h3 class=\"wp-block-heading\">I hope now you know how to actually scrape data from a website.<\/h3>\n\n\n\n<p><strong>Feel free to contact me for any question.<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey Guys,&nbsp; Today I will tell you how you can scrape a website using PHP language. To scrape a website using PHP you need to include&nbsp;simple_html_dom.php&nbsp;file in your PHP file. This file contains predefined functions to parse the html website&#8230;<\/p>\n","protected":false},"author":1,"featured_media":34,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[3,38,39],"tags":[9,5,4,6,7,10,8,11],"class_list":["post-12","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-how-to-tutorials","category-language","category-php","tag-how-to-scrape-website","tag-php","tag-scrape","tag-scrape-a-website","tag-scrape-a-website-using-php","tag-scrape-site-using-php","tag-scrape-website-using-php","tag-simple-html-dom-parser-php"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Scrape a website using PHP? - Ganofins Blog<\/title>\n<meta name=\"description\" content=\"Today I will tell you how you can scrape a website using PHP language. To scrape a website using PHP you need to include simple_html_dom.php file in your\" \/>\n<meta name=\"robots\" content=\"index, nofollow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/ganofins.com\/blog\/how-to-scrape-a-website-using-php\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Scrape a website using PHP? - Ganofins Blog\" \/>\n<meta property=\"og:description\" content=\"Today I will tell you how you can scrape a website using PHP language. To scrape a website using PHP you need to include simple_html_dom.php file in your\" \/>\n<meta property=\"og:url\" content=\"https:\/\/ganofins.com\/blog\/how-to-scrape-a-website-using-php\/\" \/>\n<meta property=\"og:site_name\" content=\"Ganofins Blog\" \/>\n<meta property=\"article:published_time\" content=\"2020-03-03T10:11:32+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-05-08T17:04:44+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/ganofins.com\/blog\/wp-content\/uploads\/2020\/03\/scrape-using-php.png\" \/>\n\t<meta property=\"og:image:width\" content=\"800\" \/>\n\t<meta property=\"og:image:height\" content=\"449\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"ganofins\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"ganofins\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/ganofins.com\/blog\/how-to-scrape-a-website-using-php\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/ganofins.com\/blog\/how-to-scrape-a-website-using-php\/\"},\"author\":{\"name\":\"ganofins\",\"@id\":\"https:\/\/ganofins.com\/blog\/#\/schema\/person\/ce75d64682bcbf85acac97c9cb93a8db\"},\"headline\":\"How to Scrape a website using PHP?\",\"datePublished\":\"2020-03-03T10:11:32+00:00\",\"dateModified\":\"2020-05-08T17:04:44+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/ganofins.com\/blog\/how-to-scrape-a-website-using-php\/\"},\"wordCount\":368,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/ganofins.com\/blog\/#\/schema\/person\/ce75d64682bcbf85acac97c9cb93a8db\"},\"image\":{\"@id\":\"https:\/\/ganofins.com\/blog\/how-to-scrape-a-website-using-php\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/ganofins.com\/blog\/wp-content\/uploads\/2020\/03\/scrape-using-php.png\",\"keywords\":[\"how to scrape website\",\"php\",\"scrape\",\"scrape a website\",\"scrape a website using php\",\"scrape site using php\",\"scrape website using php\",\"simple html dom parser php\"],\"articleSection\":[\"How To Tutorials\",\"Language\",\"PHP\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/ganofins.com\/blog\/how-to-scrape-a-website-using-php\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/ganofins.com\/blog\/how-to-scrape-a-website-using-php\/\",\"url\":\"https:\/\/ganofins.com\/blog\/how-to-scrape-a-website-using-php\/\",\"name\":\"How to Scrape a website using PHP? - Ganofins Blog\",\"isPartOf\":{\"@id\":\"https:\/\/ganofins.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/ganofins.com\/blog\/how-to-scrape-a-website-using-php\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/ganofins.com\/blog\/how-to-scrape-a-website-using-php\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/ganofins.com\/blog\/wp-content\/uploads\/2020\/03\/scrape-using-php.png\",\"datePublished\":\"2020-03-03T10:11:32+00:00\",\"dateModified\":\"2020-05-08T17:04:44+00:00\",\"description\":\"Today I will tell you how you can scrape a website using PHP language. To scrape a website using PHP you need to include simple_html_dom.php file in your\",\"breadcrumb\":{\"@id\":\"https:\/\/ganofins.com\/blog\/how-to-scrape-a-website-using-php\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/ganofins.com\/blog\/how-to-scrape-a-website-using-php\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/ganofins.com\/blog\/how-to-scrape-a-website-using-php\/#primaryimage\",\"url\":\"https:\/\/ganofins.com\/blog\/wp-content\/uploads\/2020\/03\/scrape-using-php.png\",\"contentUrl\":\"https:\/\/ganofins.com\/blog\/wp-content\/uploads\/2020\/03\/scrape-using-php.png\",\"width\":800,\"height\":449,\"caption\":\"How to scrape website using PHP\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/ganofins.com\/blog\/how-to-scrape-a-website-using-php\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/ganofins.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Scrape a website using PHP?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/ganofins.com\/blog\/#website\",\"url\":\"https:\/\/ganofins.com\/blog\/\",\"name\":\"Ganofins Blog\",\"description\":\"Learn Today Apply Today\",\"publisher\":{\"@id\":\"https:\/\/ganofins.com\/blog\/#\/schema\/person\/ce75d64682bcbf85acac97c9cb93a8db\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/ganofins.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/ganofins.com\/blog\/#\/schema\/person\/ce75d64682bcbf85acac97c9cb93a8db\",\"name\":\"ganofins\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/ganofins.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/ganofins.com\/blog\/wp-content\/litespeed\/avatar\/a1fd68d9e27b63b4007d850d5d958b59.jpg?ver=1776081109\",\"contentUrl\":\"https:\/\/ganofins.com\/blog\/wp-content\/litespeed\/avatar\/a1fd68d9e27b63b4007d850d5d958b59.jpg?ver=1776081109\",\"caption\":\"ganofins\"},\"logo\":{\"@id\":\"https:\/\/ganofins.com\/blog\/#\/schema\/person\/image\/\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Scrape a website using PHP? - Ganofins Blog","description":"Today I will tell you how you can scrape a website using PHP language. To scrape a website using PHP you need to include simple_html_dom.php file in your","robots":{"index":"index","follow":"nofollow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/ganofins.com\/blog\/how-to-scrape-a-website-using-php\/","og_locale":"en_US","og_type":"article","og_title":"How to Scrape a website using PHP? - Ganofins Blog","og_description":"Today I will tell you how you can scrape a website using PHP language. To scrape a website using PHP you need to include simple_html_dom.php file in your","og_url":"https:\/\/ganofins.com\/blog\/how-to-scrape-a-website-using-php\/","og_site_name":"Ganofins Blog","article_published_time":"2020-03-03T10:11:32+00:00","article_modified_time":"2020-05-08T17:04:44+00:00","og_image":[{"width":800,"height":449,"url":"https:\/\/ganofins.com\/blog\/wp-content\/uploads\/2020\/03\/scrape-using-php.png","type":"image\/png"}],"author":"ganofins","twitter_card":"summary_large_image","twitter_misc":{"Written by":"ganofins","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/ganofins.com\/blog\/how-to-scrape-a-website-using-php\/#article","isPartOf":{"@id":"https:\/\/ganofins.com\/blog\/how-to-scrape-a-website-using-php\/"},"author":{"name":"ganofins","@id":"https:\/\/ganofins.com\/blog\/#\/schema\/person\/ce75d64682bcbf85acac97c9cb93a8db"},"headline":"How to Scrape a website using PHP?","datePublished":"2020-03-03T10:11:32+00:00","dateModified":"2020-05-08T17:04:44+00:00","mainEntityOfPage":{"@id":"https:\/\/ganofins.com\/blog\/how-to-scrape-a-website-using-php\/"},"wordCount":368,"commentCount":0,"publisher":{"@id":"https:\/\/ganofins.com\/blog\/#\/schema\/person\/ce75d64682bcbf85acac97c9cb93a8db"},"image":{"@id":"https:\/\/ganofins.com\/blog\/how-to-scrape-a-website-using-php\/#primaryimage"},"thumbnailUrl":"https:\/\/ganofins.com\/blog\/wp-content\/uploads\/2020\/03\/scrape-using-php.png","keywords":["how to scrape website","php","scrape","scrape a website","scrape a website using php","scrape site using php","scrape website using php","simple html dom parser php"],"articleSection":["How To Tutorials","Language","PHP"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/ganofins.com\/blog\/how-to-scrape-a-website-using-php\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/ganofins.com\/blog\/how-to-scrape-a-website-using-php\/","url":"https:\/\/ganofins.com\/blog\/how-to-scrape-a-website-using-php\/","name":"How to Scrape a website using PHP? - Ganofins Blog","isPartOf":{"@id":"https:\/\/ganofins.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/ganofins.com\/blog\/how-to-scrape-a-website-using-php\/#primaryimage"},"image":{"@id":"https:\/\/ganofins.com\/blog\/how-to-scrape-a-website-using-php\/#primaryimage"},"thumbnailUrl":"https:\/\/ganofins.com\/blog\/wp-content\/uploads\/2020\/03\/scrape-using-php.png","datePublished":"2020-03-03T10:11:32+00:00","dateModified":"2020-05-08T17:04:44+00:00","description":"Today I will tell you how you can scrape a website using PHP language. To scrape a website using PHP you need to include simple_html_dom.php file in your","breadcrumb":{"@id":"https:\/\/ganofins.com\/blog\/how-to-scrape-a-website-using-php\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/ganofins.com\/blog\/how-to-scrape-a-website-using-php\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ganofins.com\/blog\/how-to-scrape-a-website-using-php\/#primaryimage","url":"https:\/\/ganofins.com\/blog\/wp-content\/uploads\/2020\/03\/scrape-using-php.png","contentUrl":"https:\/\/ganofins.com\/blog\/wp-content\/uploads\/2020\/03\/scrape-using-php.png","width":800,"height":449,"caption":"How to scrape website using PHP"},{"@type":"BreadcrumbList","@id":"https:\/\/ganofins.com\/blog\/how-to-scrape-a-website-using-php\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/ganofins.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to Scrape a website using PHP?"}]},{"@type":"WebSite","@id":"https:\/\/ganofins.com\/blog\/#website","url":"https:\/\/ganofins.com\/blog\/","name":"Ganofins Blog","description":"Learn Today Apply Today","publisher":{"@id":"https:\/\/ganofins.com\/blog\/#\/schema\/person\/ce75d64682bcbf85acac97c9cb93a8db"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/ganofins.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/ganofins.com\/blog\/#\/schema\/person\/ce75d64682bcbf85acac97c9cb93a8db","name":"ganofins","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ganofins.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/ganofins.com\/blog\/wp-content\/litespeed\/avatar\/a1fd68d9e27b63b4007d850d5d958b59.jpg?ver=1776081109","contentUrl":"https:\/\/ganofins.com\/blog\/wp-content\/litespeed\/avatar\/a1fd68d9e27b63b4007d850d5d958b59.jpg?ver=1776081109","caption":"ganofins"},"logo":{"@id":"https:\/\/ganofins.com\/blog\/#\/schema\/person\/image\/"}}]}},"amp_enabled":true,"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/ganofins.com\/blog\/wp-json\/wp\/v2\/posts\/12","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ganofins.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ganofins.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ganofins.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ganofins.com\/blog\/wp-json\/wp\/v2\/comments?post=12"}],"version-history":[{"count":1,"href":"https:\/\/ganofins.com\/blog\/wp-json\/wp\/v2\/posts\/12\/revisions"}],"predecessor-version":[{"id":170,"href":"https:\/\/ganofins.com\/blog\/wp-json\/wp\/v2\/posts\/12\/revisions\/170"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ganofins.com\/blog\/wp-json\/wp\/v2\/media\/34"}],"wp:attachment":[{"href":"https:\/\/ganofins.com\/blog\/wp-json\/wp\/v2\/media?parent=12"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ganofins.com\/blog\/wp-json\/wp\/v2\/categories?post=12"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ganofins.com\/blog\/wp-json\/wp\/v2\/tags?post=12"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}