<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Nyv Mondele Mbola on Medium]]></title>
        <description><![CDATA[Stories by Nyv Mondele Mbola on Medium]]></description>
        <link>https://medium.com/@supernyv?source=rss-975701029a1e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*w8HCY6MKOCA457J5</url>
            <title>Stories by Nyv Mondele Mbola on Medium</title>
            <link>https://medium.com/@supernyv?source=rss-975701029a1e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 23:22:14 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@supernyv/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[A Data Science Case: From Data Gathering to Visualization]]></title>
            <link>https://medium.com/@supernyv/a-data-science-case-from-data-gathering-to-visualization-5dcfab13d363?source=rss-975701029a1e------2</link>
            <guid isPermaLink="false">https://medium.com/p/5dcfab13d363</guid>
            <category><![CDATA[data-visualization]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[web-scraping]]></category>
            <category><![CDATA[regex]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Nyv Mondele Mbola]]></dc:creator>
            <pubDate>Sat, 03 Sep 2022 19:52:23 GMT</pubDate>
            <atom:updated>2024-06-22T11:38:35.418Z</atom:updated>
            <content:encoded><![CDATA[<h3>A Data Science Case: From Data Collection to Exploratory Data Analysis</h3><h4>An illustration of how data is collected, cleaned, analyzed, and visualized.</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/553/1*P2OWiTffdupVjv4DLe1dWw.png" /><figcaption>Image by Author.</figcaption></figure><p>You probably have heard the word “data science” so many times that you feel you should know what it is all about, but you still have no idea what it really is, no matter how many definitions you have read on the internet. Don’t worry, you are not alone. That is why I will walk you through a small project to show what a data science project might look like. Not all data science projects look the same, as there are many different methods and tools that can be used to do the same things, and there are many different problems and each might require different tools, approaches and methods. But this short practice-oriented article will give you an idea about what kind of tasks are performed in a data science project, why, and how.</p><p>What is data science? In simple terms, Data Science can be defined as the process of extracting meaning and insight from data. Typically, these are the questions we need to ask in a data science project:</p><ul><li>What problem(s) are we trying to solve or what facts are we trying to understand?</li><li>Where to get the data we need for this?</li><li>How to extract the data from its source?</li><li>Do we need to clean our data? If so, how do we do that?</li><li>How can we manipulate the data to get answers to our questions?</li></ul><p>Something important to note is that it is also possible to start with the data and come up with a problem or insights while manipulating it, but in most cases we start with a problem. With that in mind, let’s get started.</p><p>In this project we will try to analyze the performance of Congolese football clubs and compare them.</p><p><strong>Part I: Formulating the Problem.</strong></p><p>How have local football clubs in Congo performed in the last few years and what factors could contribute to their good or bad performance? Great football countries like France, Germany, Brazil, and so on have many great football clubs, with highly ranked competitions watched on TV by millions of people around the world, and this certainly contributes to the performance of their national teams. If we know how these clubs have performed in the last few years, we probably can understand the performance of the Congolese national football team. One way to analyze football teams performance is by using the <strong><em>winning percentage</em></strong>, which is the number of game won divided by the total number of games played<strong><em>.</em></strong></p><p><strong>Part II : Getting the Data.</strong></p><p>It becomes obvious that to get answers to our questions we need data. We need to identify the sources of our data and the easiest means to retrieve it. For this project we will web scrape <a href="http://soccerstand.com,/">http://soccerstand.com,</a> going directly to the result pages of the football clubs for which we need the data. We divide this part into two steps: Step 1 for getting the raw data from each page, and Step 2 for parsing each page to retrieve the games data we want.</p><p>An important step to the scraping process is the inspection of our webpage to know where the data and all html elements we will need to manipulate are located. I’ll not go into details on how to do that here but remember that this is how the <strong><em>html tags’</em></strong> <strong><em>class names</em></strong> and <strong><em>ids</em></strong> that will be used in this part of the article were found.</p><p><strong><em>Step 1: Retrieving the data from the webpage</em></strong></p><p>Let’s do all the imports:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/506/1*cV2-08DGAYcjh-HTQjud5A.png" /><figcaption>Importing all the components we will need.</figcaption></figure><p>These lines will be explained as we use the components we have imported.</p><p>We will use <strong><em>selenium</em></strong> to interact with the web browser as the site we will scrape is a <strong><em>dynamic website</em></strong> (some content is displayed only as the user interacts with the browser, for example by scrolling or clicking buttons to load data). We will need to give our browser some time to load data before clicking on any buttons, just like humans do, so let’s set the maximum time we can wait for data to load on a page : max_pause = 60 #seconds</p><p>And let’s store the URLs of the pages we will scrape in a list because we need the data for all our clubs of interest and they are not all in a single place:</p><pre>pages <strong>=</strong> [<br>&quot;https://www.soccerstand.com/team/otoho-d-oyo/0GuF1aSS/results/&quot;,<br>&quot;https://www.soccerstand.com/team/leopard/rulf0ilK/results/&quot;,<br>&quot;https://www.soccerstand.com/team/diables-noirs/xlWrOB3I/results/&quot;,<br>&quot;https://www.soccerstand.com/team/etoile-du-congo/dISk7gAI/results/&quot;,<br>&quot;https://www.soccerstand.com/team/cara-brazzaville/AutlQkBd/results/&quot;<br>]</pre><p>Now we define a function that will take each of the links in the list as a parameter and open the browser to that page for us:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/264/1*pwoVVw6aC1dKVmjEBL2muA.png" /><figcaption>The function to open the browser and go a web page.</figcaption></figure><p>When we open the webpage we might have to click on a banner to get it out of our way. We define a function that will take a browser object (the currently opened webpage) and click on any banner (which is an html element) with the id <strong><em>‘onetrust-accept-btn-handler’</em></strong>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/601/1*RHJjBzHAJN4ddEuSwHV-Wg.png" /><figcaption>The function to click on any banner on the page.</figcaption></figure><p>Notice the use of <strong><em>WebDriverWait</em></strong> and the expected conditions (<strong><em>EC</em></strong>) methods we imported earlier. Their use above simply means when we start the browser and open our page, we wait until the element with the id <strong><em>‘onetrust-accept-btn-handler’ </em></strong>appears. Once the element is present, we <strong><em>click</em></strong> on it. But we won’t wait forever, and the element might actually never appear, so we use the <strong><em>max_pause</em></strong> variable we defined earlier (remember we set it very high to 60 seconds, but you can change that to fit your situation). After 60 seconds, if no banner appeared, our function prints “<strong><em>No Banner Appeared</em></strong>” and returns nothing.</p><p>Now let’s assume our webpage is displayed with no banner and we can scroll down and view everything we want. So we define a function that does just that, load the full webpage, scrolls down till the bottom of our page is reached and stop when there is nothing more to load, just like a human would do:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/755/1*kqejbgTwpJMw3YQJwki8ww.png" /><figcaption>The function to lead the page in its full height.</figcaption></figure><p>I tried to make that function as self explanatory as possible.</p><p>Now define another function that will call the above functions in the required order to load the data for a page for which the URL is passed:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/463/1*i9UglJ3kDO41H4ON_0dNbw.png" /><figcaption>The function to retrieve the html data.</figcaption></figure><p>So, for each page in our links list (the variable we defined as <strong><em>pages</em></strong> earlier) we call the function <strong><em>get_data() </em></strong>to retrieve pages for each one of our football club. For this, we use list comprehension so all the pages are stored in a list we will call <strong><em>sources</em></strong>:</p><pre>sources <strong>=</strong> [get_data(team) <strong>for</strong> team <strong>in</strong> pages]</pre><p>We run the code and sit back. The process takes about 10 minutes, depending on your internet speed. The image bellow shows a portion of the result from our code:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/313/1*cBKkw_DGkAjxHWbxnUwC4A.png" /><figcaption>A screenshot of the output for one team. All the others look similar</figcaption></figure><p><strong><em>Step 2: Now we parse the html content for each site we scraped</em></strong></p><p>All our pages have been stored in a list (<strong><em>sources</em></strong>). We use another list comprehension to get Soup objects for each of these pages and store them in another list we will call <strong><em>soups</em></strong>:</p><pre>soups <strong>=</strong>[BeautifulSoup(source, &quot;html.parser&quot;) <strong>for</strong> source <strong>in</strong> sources]</pre><p>The html contents for all these pages have the same structure, so we can retrieve our data the same way for all the clubs. This means we once again inspected the html text for all the pages and we know where the data we need is located and which tags we will need to manipulate. We write a function to do the parsing:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/951/1*LlicqfG6Vm0S8WLCqsEREw.png" /><figcaption>The function to create Data Frames with Data for each team.</figcaption></figure><p>The function can then be used and reused. We will call it for each club, then save the data we scraped in csv files for future uses (we won’t need to scrape again when we need the same data).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/441/1*AqsqWS5oD8GwNJpmx8uR3A.png" /><figcaption>Call the function on each soup object for each club, and save the data to csv files.</figcaption></figure><p>Now we have our data. The next stage is Exploratory Data Analysis (EDA), where we need to find out if the data needs some cleaning and if it can help us answer our questions. We will divide that stage into two parts, the first part for Cleaning and the second for Analysis and Visualization.</p><p><strong>Part III : Cleaning the Data</strong></p><p>This part largely depends on your domain knowledge and your understanding of the data you have. We examined the data and found out some names contain parenthesis, slashes and so on and we don’t need those. So we clean the names but we make sure we keep names that have bars (-) and dots (.). We write a function to do that:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*iZz8dYcAt7jiAUJBXziQ9w.png" /><figcaption>The function to clean clubs names</figcaption></figure><p>First we define a regex pattern to match any name that contains one or many words optionally followed by or separated by any number of spaces, dots, or bars. That means if there is anything else inside a text, like parenthesis, our pattern will only match the part that does not have that thing.</p><p>The parenthesis <strong><em>(…)</em></strong> in the regex pattern means we will take whatever the pattern matches as a group and the <strong><em>?P&lt;name&gt;</em></strong> means we will name the captured group as <strong><em>name</em></strong>. The group we will capture is written inside the square brackets. We will test if our function works as we want with assertions:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/708/1*EvI1kgmFEOAu4QSW14Z5vg.png" /><figcaption>Assertions to test our name cleaning function</figcaption></figure><p>We also found out the matches played in the current year have unusual formats. We also need to clean the date:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/364/1*sI-5xNAmTtCns1FLm-RD6Q.png" /><figcaption>The function to clean the dates.</figcaption></figure><p>We defined 3 patterns for 3 different formats. The first is for dates with digits like “<strong><em>dd.dd. dd:dd</em></strong>” where we only need the “<strong><em>dd.dd</em></strong>” part. So our regex uses a <strong><em>positive lookahead (?= )</em></strong> to get “one or more digits followed by a dot and then by one or more digits” (which we will take) followed by a space (which we will not take).</p><p>The second pattern is for dates with digits like “<strong><em>dd.dd.dddd</em></strong> Something else” where we only need the “<strong><em>dd.dd.dddd</em></strong>” part. This regex also uses a <strong><em>positive lookahead (?=\w+)</em></strong> to take only the part we want.</p><p>The third patter is for dates with digits like “<strong><em>dd.dd.dddd</em></strong>” alone.</p><p>For each matching pattern, we change to a <strong><em>day/month/year</em></strong> format.</p><p>We test our function:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/620/1*t99lbagUfXIRiahp08qLXA.png" /><figcaption>Assertions to test our date cleaning function.</figcaption></figure><p>Now we can clean all the names and dates at once. First we concatenate all the data from all the teams, then we clean the names and dates, change the Event Date column into a datetime type and finally change the names for <strong><em>Republic of the Congo</em></strong> and <strong><em>Afric</em></strong>a. We also take care of saving the new data frame for future needs:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/806/1*33MPJI8QcjKtzvewmxsE9w.png" /><figcaption>Actual cleaning of the clubs names and match dates, and a few more manipulations.</figcaption></figure><p>To see the result of all this cleaning we display the first 10 rows of our data frame with <strong><em>all_df.head(10)</em></strong>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/813/1*DN2Mv7qBRE78-o0_5pAxZA.png" /><figcaption>The first 10 rows of our Data Frame.</figcaption></figure><p><strong>Part IV : Analyzing and Visualizing the Data</strong></p><p>We noticed team did not exist before 2017, so if we will compare our teams we need to use the period they all have data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/543/1*0n7t53eSPVsU3bJZVWP4wg.png" /><figcaption>Selecting only the games played after 2016 for all teams.</figcaption></figure><p>We might also want to know if playing home or away have any impact on the match’s outcome. We will not go into such details here but let’s see how we could split our data for that:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/878/1*RV3hBKUhxh-tAU6NfaGccQ.png" /><figcaption>Splitting Home and Away games.</figcaption></figure><p>We will keep these two data frames just in case we ever need them. But for now let’s create another data frame that have all the data for the period after 2016 by concatenating our <strong><em>home</em></strong> and <strong><em>away</em></strong> data frames. Notice we changed columns <strong><em>Team Away</em></strong> and <strong><em>Team Home</em></strong> to <strong><em>Team.</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/533/1*BoyESjP_H7DtOlXYiYpwNA.png" /></figure><p>Now let’s group the matches by Team, Year, and Results and count each group:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/736/1*PsVLW2FX_7uHEqAVnbVb5w.png" /></figure><p>This means for each team we will count each match outcome for each year. We also name the column with the count as ‘<strong><em>Game Count</em></strong>’.</p><p>Next we calculate the Winning Percentage per year by dividing the total number of game won by the total number of game played for each team for each year:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/815/1*r7sW28mDn8VhIB3oA_wnzg.png" /></figure><p>We print the first 10 rows of our new data frame with <strong><em>wins_summary.head(10)</em></strong>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/387/1*pnMs24qxNg9uQdp31WnKOQ.png" /></figure><p>From that table we can already start drawing some conclusions but we might not be able to see clearly what the data is telling us. So it’s time to visualize the data. We will first add a column to store a color for each team.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/669/1*dqFXVOnHo8rTNO0cq3uuXA.png" /></figure><p>Now we can plot our data:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/724/1*WhauOl2x3ftiRygXWW0bOA.png" /></figure><p>Running that code gives the result below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/746/1*NYtH4AoeyUgDWNWjrROV8A.png" /></figure><p><strong>Part V: What questions did we answer?</strong></p><p>This is where we report on the findings from our analysis. We can put this into a report and the structure will vary depending on the purpose of the project. A must have skill here is story telling. Most people don’t need or want to understand all the methods and tools we used but they certainly want to understand what is the meaning of all these ? Why did we have give ourselves so much pain writing long codes and doing massive thinking? We need to interpret the result and perhaps give some recommendations based on our analysis.</p><p>I’ll leave this to you to say what you think the data is telling us about our teams’ performance and what could be possible causes of bad performance.</p><p>That’s it for this project. As you can see, there was no Machine Learning involved, not all problems need Machine Learning as it all depends on our goals.</p><p>The Notebook and scraped data for this article can be found <a href="https://github.com/supernyv/Data_Science_Projects/tree/main/Congolese%20Football">here</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5dcfab13d363" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Impossible Web Scraping]]></title>
            <link>https://medium.com/@supernyv/the-impossible-web-scraping-cfe9444d1d5e?source=rss-975701029a1e------2</link>
            <guid isPermaLink="false">https://medium.com/p/cfe9444d1d5e</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[beautifulsoup]]></category>
            <category><![CDATA[selenium]]></category>
            <category><![CDATA[web-scraping]]></category>
            <dc:creator><![CDATA[Nyv Mondele Mbola]]></dc:creator>
            <pubDate>Sun, 19 Jun 2022 13:44:57 GMT</pubDate>
            <atom:updated>2022-07-02T18:13:17.982Z</atom:updated>
            <content:encoded><![CDATA[<h4>Scraping a dynamic website with Selenium and BeautifulSoup in Python</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/612/1*iy9wt9wyltgl_DGtuXMOEg.jpeg" /><figcaption>A frustrated person asks for help (Photo: Yahoo! Finance)</figcaption></figure><p>To do data science we need data, of course. But getting the data is not always an easy task. One way data scientists get data is through <a href="https://www.parsehub.com/blog/what-is-web-scraping/">web scraping</a>, which is the process of extracting data from a webpage. But some websites seem impossible to scrape, until we find a way to do it.</p><p>In this article, I’ll explain in details how to get data from a seemingly impossible to scrape website using Selenium and BeautifulSoup. Selenium is a library used to control the browser, while BeautifulSoup is a library used to parse html files. The site I’ll be scraping is <a href="https://money.usnews.com/careers/best-jobs/rankings/the-100-best-jobs">here</a>, for the 100 best jobs in the United-States.</p><p>Why call it the impossible web scraping? Because we could either be dealing with a dynamic website, or with a site that simply detects automation and blocks access to some features and data, or we could be dealing with a site that does all of these. On one hand, dynamic websites interact with the user, and only produce some results when some actions are performed by the user, like scrolling, clicking buttons, hoovering and so on, which is why it can be very hard to scrape these sites. On the other hand, when a site is configured to block access to its content when automation is detected, the task gets even more complicated. But with the right methods and tools, nothing can stop a data scientist from getting the data they need.</p><p>Here is the general web scraping process we will go through for this task:</p><ol><li>Visit the website to be scraped and Inspect the elements of interest to find out where the data is located;</li><li>Use Selenium to interact with the website and retrieve the html text;</li><li>Use BeautifulSoup to parse the html text and retrieve the target data;</li><li>Save the data in a desired format.</li></ol><p>Now we will go through this process in detail.</p><ol><li><strong>Visit the website, and inspect the elements.</strong></li></ol><p>The goal of this step is to help us know how the website interacts with the user and where our data is located in the html text.</p><p>In our example, this is how the website interacts with the user:</p><ul><li>When the page loads, the first 10 Jobs are displayed at first. Then we need to scroll down to load more jobs.</li><li>We reach the maximum number of jobs displayed on scrolling down; then a “<em>Load More</em>” button appears, it automatically changes into “<em>Loading</em>” and few more jobs are loaded.</li><li>The previous step is repeated until 50 jobs are displayed.</li><li>Then the “Load More” button no longer turns into “Loading”, so the user needs to click on it to load more jobs.</li><li>Now the remaining jobs are displayed as we scroll down like before.</li></ul><p>So our automation process will need to mimic us and follow these steps.</p><p>Now we inspect one of the jobs displayed by right-clicking on it and selecting “inspect”. All the other jobs are in the same format, they’re contained in a <strong><em>div</em></strong> tag with <strong><em>class = “card-content-container”</em></strong>. For each Job, the data we’re interested in are the <strong>Job Title</strong>, <strong>Rank</strong>, <strong>Projected Jobs</strong>, <strong>Median Salary</strong>, and <strong>Education Needed</strong>. Read <a href="https://www.w3schools.com/html/html_intro.asp">here</a> for basic intro on html if needed.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UdioVqcgvlgAN_hxREQIkA.jpeg" /><figcaption>Part of the webpage on the left with our target data highlighted in red boxes. Inspected element on the right with target html tag highlighted in red box.</figcaption></figure><p>Once we unroll the <em>div tag</em> with <strong><em>class = ‘card-content-container’</em></strong> in the newly opened <strong><em>“inspect element”</em></strong> window, we see the html section we are interested in. Below is a simplified version of what it looks like:</p><figure><img alt="The data we need are highlighted in linked red boxes. We can clearly see which html tags contain them." src="https://cdn-images-1.medium.com/max/471/1*haRtMpc5dQY3JxzdAw2ULw.png" /><figcaption>The data we need are highlighted in linked red boxes. We can clearly see which html tags contain them.</figcaption></figure><p>2. <strong>Use Selenium to interact with the website and retrieve the html content.</strong></p><p>Now that we know what we need to do on the website and where to find our data in the html text, we go to our script and write the codes.</p><p>First we do all the imports.</p><pre>from selenium import webdriver<br>from bs4 import BeautifulSoup<br>import time<br>import pandas as pd</pre><p>Check <a href="https://tilburgsciencehub.com/building-blocks/configure-your-computer/task-specific-configurations/configuring-python-for-webscraping/">here</a> for how to install Selenium, <a href="https://www.selenium.dev/documentation/webdriver/getting_started/install_drivers/">here</a> to get the drivers, and <a href="https://tilburgsciencehub.com/building-blocks/configure-your-computer/task-specific-configurations/configuring-python-for-webscraping/">here</a> again for how to place the drivers in your computer’s PATH. Feel free to use any other resources and methods that work for you, but all these steps are necessary to have a working automated scraper with Selenium.</p><p>Next in our code we get the site’s URL in a variable. Let’s name it url:</p><pre>url = &quot;<a href="https://money.usnews.com/careers/best-jobs/rankings/the-100-best-jobs/">https://money.usnews.com/careers/best-jobs/rankings/the-100-best-jobs/</a>&quot;</pre><p>It seems the site we’re scraping detects when automation is being used, so the hidden data would not appear after scrolling. I headed to the site : <a href="https://piprogramming.org/articles/How-to-make-Selenium-undetectable-and-stealth--7-Ways-to-hide-your-Bot-Automation-from-Detection-0000000017.html">12 Ways to hide your bot automation</a> to figure out how to overcome that. So, in the first part of this step, we set all the options and get all the parameters we need, and we define a function we call <strong><em>load()</em></strong> that will use these parameters and methods to scroll down the page, as shown below :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/718/1*8GBxEY2uq3h4dt0z2vG0_Q.png" /><figcaption>Getting the Webdriver and Defining the Function to Load the page</figcaption></figure><p>From there we simply use the <strong><em>load()</em></strong> function defined above. We already set the function to return 0 when the page height has not changed after we call <strong><em>driver.execute_script()</em></strong> a second time.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/987/1*U0nTadhVpF6R0Cw5OaGQcA.png" /></figure><p>Listen, I know it’s a bad idea to use a naked <em>except statement</em>, but for the sake of keeping things simple here let’s just move along, thanks.</p><p>This is where the magic happens. In brief, we use a while loop to call our <strong><em>load()</em></strong> function and keep calling it repeatedly as long as it returns something other than <em>0</em>. But when the function does return <em>0</em> the first time we know we either reached the bottom of the page and it’s time to click on the <strong><em>Load More</em></strong> button or something went wrong. So we try to click on the load more button using <strong><em>driver.find_element_by_xpath()</em></strong> to locate the button and <strong><em>.click()</em></strong> right away to click on it. If the button is there, the page will load more data and we need to call our <strong><em>load()</em></strong> function again to scroll down the page again; although we could have left this call out as the while loop will resume anyways, calling the <strong><em>load()</em></strong> function at the beginning, but we need to account for delays that might be caused by the browser and make sure we actually scroll every time. The While loop will continue until the height of the page is not changing anymore. Shown below is a screenshot of what I got after running the code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/457/1*hpvi9x4_uEMogiEqRIl3pA.png" /><figcaption>The output from the script.</figcaption></figure><p>The data from this step is automatically stored for us in <strong><em>driver.page_source</em></strong> by Selenium. So we use BeautifulSoup now.</p><p><strong>3. Use BeautifulSoup to parse the html file and retrieve the target data.</strong></p><p>First load the data in a soup object:</p><pre>soup = BeautifulSoup(driver.page_source, &quot;html.parser&quot;)</pre><p>Next, we create a Pandas Dataframe that will store the data once retrieved from the html text. We take care of labelling the columns at the same time. We then take all the jobs on this page using <strong><em>soup.findall()</em></strong> method:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1006/1*RNgc_YQ8GIhaIDc0DqsOkA.png" /></figure><p>This returns a list of html texts that met our demand. We then parse each text individually using a for loop, setting the conditions to add the data to our Pandas Dataframe:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1006/1*ZKJ5bYEZ5h2u8crWWqnuMw.png" /></figure><p>The <strong><em>index</em></strong> variable is used to add the data to the data frame rows. Note that <strong><em>”””Remove comma →”””</em></strong> is only used as a comment to indicate what the pointed expression is doing, and the semicolon (;) must be there. Not neat and probably not a good way to used the triple quotes but I simply tried to avoiding a very complicated/unclear expression like <strong><em>“”.join((data[0].a.string).split(“,”))</em></strong> and still have the variable on a single line like all the other ones.</p><p><strong>4. Save the data in a desired format.</strong></p><p>Let’s save our data as a csv file for example:</p><pre>df<strong>.</strong>to_csv(&quot;best_jobs_scraped.csv&quot;, index<strong>=False</strong>)</pre><p>We can see the first 10 rows of our data:</p><pre>df.head(10)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/598/1*HaHw7g3PpCTrZiIs4YVj8g.png" /><figcaption>The first 10 Rows of our Dataframe displayed with df.head(10)</figcaption></figure><p>That’s all for now!</p><p>Feel free to download the notebook and csv file for this project <a href="https://github.com/supernyv/Data_Science_Projects/tree/main/Factors%20Influencing%20Salaries">here</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cfe9444d1d5e" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>