2

I'm having trouble figuring out how to use PHP Simple HTML DOM Parser for pulling information from a website.

require('simple_html_dom.php'); $html = file_get_html('https://example.com'); $ret = array(); foreach($html->find(".project-card-mini-wrap") as $element) { echo $element; } 

The output of $element is:

<div class="project-card-mini-wrap"> <a class="project_item block mb2 green-dark" href="/projects/andrewkostirev/kostirev-the-real-you"> <div class="project_thumbnail hover-group border border-box mb1"> <img alt="Project image" class="hover-zoomin fit" src="https://ksr-ugc.imgix.net/projects/2123706/photo-original.png?v=1444253259&amp;w=218&amp;h=162&amp;fit=crop&amp;auto=format&amp;q=92&amp;s=9d6c437e96b720dce82fc9b598b3e8ae" /> <div class="funding_tag highlight">10 days to go</div> <div class="hover-zoomout bg-green-90"> <p class="white p2 h5">A clothing brand like never seen before</p> </div> </div> <div class="project_name h5 bold"> KOSTIREV - THE REAL YOU </div> </a> </div> 

This is the information I'd like to pull from the website:
1: Link href
2: Image src
3: Project name

3
  • 1
    I'm voting to close this question as off-topic because SO is not a code writing service. Commented Nov 20, 2015 at 8:18
  • upload, or download? Commented Nov 20, 2015 at 8:18
  • i have tried every thing as in First answer Commented Nov 20, 2015 at 8:59

3 Answers 3

2

Hopefully this will provide some insight to you as well as other users of PHP Simple HTML DOM Parser

foreach($html->find(".project-card-mini-wrap") as $element) { echo "Project name: ",$element->find('.project_name',0)->innertext,"<br/>\n"; echo "Image source: ",$element->find('img',0)->src,"<br/>\n"; echo "Link: ",$element->find('a',0)->href,"<br/>\n"; } 

Produces this output:

Project name: KOSTIREV - THE REAL YOU Image source: https://ksr-ugc.imgix.net/projects/2123706/photo-original.png?v=1444253259&w=218&h=162&fit=crop&auto=format&q=92&s=9d6c437e96b720dce82fc9b598b3e8ae Link: /projects/andrewkostirev/kostirev-the-real-you 
Sign up to request clarification or add additional context in comments.

2 Comments

tried this but i am getting this error Notice: Trying to get property of non-object
@MArfan The find() function requires a second parameter, use 0 to grab the first element it finds. Example updated
-1

I tried this and it worked, thanks for the help! Here is something i made using primewire.ag as a example.... The goal here was to extract all the links of a given page.

<?php require('simple_html_dom.php'); // Create DOM from URL or file $html = file_get_html('http://www.primewire.ag/watch-2805774-Star-Wars-The-Last-Jedi-online-free'); // Find All Movie Links $linkPrefix = 'http://primewire.ag'; $linkClass; foreach($html->find(".movie_version_link") as $linkClass) { echo "Link: ",$linkPrefix,$linkClass->find('a',0)->href,"<br/>\n"; } ?> 

Comments

-2

This is also a good library for scraping and traversing via HTML

https://github.com/paquettg/php-html-parser

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.