Scraping Websites with SimpleHTMLDom in PHP

I am struggling to extract specific data from a table on a website page, particularly the columns name, level, and experience. The table's alternating row colors (zebra pattern) are complicating this task.

I have implemented SimpleHTMLDom for this purpose, and below is the code I am currently using:

<?php
include('simple_html_dom.php');

function scrapeHighscore($world) {
    $html = str_get_html('https://secure.tibia.com/community/?subtopic=highscores&world=' . $world);
    $tables = $html->find('table');
    foreach($tables as $table) {
        $names = $table->find('a');
        foreach($names as $name) {
            return $name . ' - ';
        }

        $levels = $table->find('td[@width="15%"]');
        foreach ($levels as $level) {
            return $level . ' - ';
        }

        $experiences = $table->find('td[@width="20%"]');
        foreach ($experiences as $experience) {
            return $experience . '<br>';
        }
    }
}

echo scrapeHighscore('Antica');

?>

You can check out an example of the website I am trying to scrape here:

The desired output includes only the names, levels, and experiences from the table in the format shown below:

Meendel 536 2538560931
Drendaric   514 2237710308
Magicalse   509 2180132453
...
King Migoon 446 1460356304

I am currently facing issues with my script not returning any results and instead displaying a blank page. How can I modify my approach to achieve the desired outcome?

Answer №1

function retrieveTopPlayerInfo($server) {
    $output = '';
    $html = str_get_html('https://secure.tibia.com/community/?subtopic=highscores&world=' . $server);
    $tables = $html->find('table');
    foreach($tables as $table) {

        $names = $table->find('a');
        foreach($names as $name) {
            $output.= $name . ' - ';
        }

        $levels = $table->find('td[@width="15%"]');
        foreach ($levels as $level) {
            $output .= $level . ' - ';
        }

        $experiences = $table->find('td[@width="20%"]');
        foreach ($experiences as $experience) {
            $output .= $experience . '<br>;
        }
    }
    return $output;
}
echo retrieveTopPlayerInfo('Antica');

This function only displayed the first player's name, which was not the intended result.

If no information is displayed, try the following:

$html = str_get_html('https://secure.tibia.com/community/?subtopic=highscores&world=' . $server);
echo $html;

If nothing is echoed, investigate how str_get_html functions and troubleshoot any potential issues.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What could be causing my divs to overlap?

I recently started coding and I'm facing an issue with my <header> and <section> divs overlapping each other. I was under the impression that being block elements, <section> should be displayed below <header>. Any suggestions o ...

Utilize PHP to generate a table from data

I successfully created a table using information retrieved from my database, and everything is displaying as intended. However, I am facing an issue where adding a background color applies it to every row in the table. How can I add a background color wit ...

Unusual shadow cast by the box's silhouette

I am currently facing an issue with a box and its shadow. When I close the box, a different shadow lingers behind. I have tried troubleshooting this problem but cannot pinpoint the source. I have included the relevant code files in the specified folders. I ...

Adding input values to a jQuery Ajax POST request

I am currently attempting to send form values to a remote API using AJAX. The necessary information I require from the HTML form element includes the author, string, false, and true. At the moment, I have hard-coded some values but... function sendData ...

Having trouble extracting images from Pixiv using a Python 3 scraper with Selenium, BeautifulSoup4, and urllib.request?

Downloading images from proved to be a bit tricky for me. I had to log in just to start extracting details from the pages. However, every time I attempted to use urllib.request.urlretrieve, I encountered a 403 forbidden error. I scoured the internet for a ...

PHP mail function causing email to be filtered as spam

I've been using this code to send emails, but unfortunately, the emails are ending up in the spam folder. I've tried sending through both Gmail and Hotmail, with the same result. Here's the PHP code: <?php if($_POST) { //check if its an ...

Is there a way to display one of the divs in sequence each time the page is reloaded?

Is there a way to display the divs sequentially when the page is refreshed? For instance, on the initial load, only div 1 should appear, and with each refresh it should cycle through divs 2, 3, 4, and 5 before starting over. Below is an example I'm c ...

Resolve the 'undefined offset error' in Drupal Views Nivo Slider

After updating to the latest version of Drupal, I encountered an error while trying to use the "Views Nivo Slider" module. I keep seeing this error message: Notice: Undefined offset: 0 in template_preprocess_views_nivo_slider_view_nivo_sliderfields() (lin ...

Tips for positioning text on the left and right sides of a div in HTML styling

I am struggling with positioning two pieces of text within a div. Despite having some styling already in place, the text is currently displaying one after the other on the left-hand side. I want to position one piece of text to the left and another to the ...

A minimalist dropdown menu using only HTML and CSS with an onclick function

I have been working on creating an onclick dropdown menu for mobile devices that should disappear when viewed on wider screens, but I've encountered an issue where the links in the menus are not clickable even though the @media query is set up correct ...

Mastering the art of displaying a progress bar using ajax, php, and mysql

I am facing an issue with querying a large amount of data from the database and displaying a progress bar. Even though my code successfully retrieves data from the server, the progress bar immediately jumps to 100% without updating while the Ajax query is ...

What is the best way to extract the chosen value from a dropdown menu within the $_POST array?

In my HTML code, I have a droplist using the <select> tag. How can I retrieve the selected value from the $_POST array once the user submits the form? <form action="subj_exec.php"> <?php echo $_SESSION['SESS_MEMBER ...

Events that are loaded using jQuery

My webpage features dynamic content generated by PHP, and I am looking to utilize jQuery's load function to refresh the div container periodically. This will ensure that new content is displayed on top of the existing content. The jQuery function I a ...

My confusion is running high when it comes to navigating the mobile version of my website

Creating a navigation bar for my website has been challenging. Whenever I switch to mobile view, the list in my navigation bar shifts 40px to the right side of the screen where there's nothing. Is there any way you can assist me with this issue? ...

Getting JSON via AJAX with the `&` symbol in Joomla

Here's the string output after being passed through JSON.stringify: "chk={ "node":"obj.1.46971", "user":"arslan", "to":"mercedes", "article":"<p>this my test&nbsp;</p>", "subject":"haha", "comments":"hello" }" ...

Conditional validation for uploading image files using Yii2

As a beginner in Yii, I am seeking guidance on applying image validation for .svg and .png files only when the user selects an image. public function rules() { return [ [['logo'], 'file', 'extensions'=&g ...

Error: content within v-html directive not displaying as expected

The v-html content is not rendering correctly for me. Interestingly, when I remove the v-html in the first list item, it works fine. Take a look at my code below: <div> <br> <li v-html="`a`"> <ul> <li ...

Error encountered when executing Selenium WebDriver

Currently in the middle of an online Python course and making progress, but encountering a challenge with pulling HTML data. The TypeError that occurs every time the process finishes has me puzzled. I've followed the instructor's steps closely, y ...

Struggling with setting up a search bar for infinite scrolling content

After dedicating a significant amount of time to solving the puzzle of integrating infinite scroll with a search bar in Angular, I encountered an issue. I am currently using Angular 9 and ngx-infinite-scroll for achieving infinity scrolling functionality. ...

Exploring various perspectives in CodeIgniter

I am currently working on a Codeigniter project, and I have encountered a situation where I need to access a variable that has been set in one view from another view. Is it possible to achieve this within the Codeigniter framework? Below is the code snipp ...