<?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 Lee Mark Chavez on Medium]]></title>
        <description><![CDATA[Stories by Lee Mark Chavez on Medium]]></description>
        <link>https://medium.com/@leecvz?source=rss-dca7f677103a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*P5pDPUlWhyaSw4IJQDJPPw.jpeg</url>
            <title>Stories by Lee Mark Chavez on Medium</title>
            <link>https://medium.com/@leecvz?source=rss-dca7f677103a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 13:40:24 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@leecvz/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[I used Selenium to make a bot that scrapes data from JobStreet (Full 2022 walkthrough).]]></title>
            <link>https://leecvz.medium.com/i-used-selenium-to-make-a-bot-that-scrapes-data-from-jobstreet-full-2022-walkthrough-1791400a7513?source=rss-dca7f677103a------2</link>
            <guid isPermaLink="false">https://medium.com/p/1791400a7513</guid>
            <category><![CDATA[job-hunting]]></category>
            <category><![CDATA[selenium]]></category>
            <category><![CDATA[automation]]></category>
            <category><![CDATA[web-scraping]]></category>
            <category><![CDATA[data-science]]></category>
            <dc:creator><![CDATA[Lee Mark Chavez]]></dc:creator>
            <pubDate>Sat, 05 Nov 2022 08:10:38 GMT</pubDate>
            <atom:updated>2022-11-07T07:38:12.842Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Tip:</strong> you can skip ahead to step 3 for that juicy code walkthrough.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JXR4OB9p-alfeo0OJis-PQ.png" /><figcaption>A photo of “a bot looking at a job-postings” generated by <a href="https://openai.com/dall-e-2/">DALL-E-2</a></figcaption></figure><blockquote>Web scraping is “resource exhausting”. It<strong> </strong>can exhaust your time, your tears, and whatever resource it is that that kinda makes you a pleasant person to talk to. But despite all that, <strong>Web scraping is a great skill to have</strong>.</blockquote><p>Re-reading the title of this post made me think if it sounds like an admission of guilt to a cybercrime. After all, web scraping has often been considered to be legally in the grey area. However, the recent landmark case between <a href="https://www.natlawreview.com/article/hiq-labs-v-linkedin"><strong>hiQ vs. LinkedIn</strong></a><strong> has confirmed that web scraping is indeed, legal.</strong></p><p>Employment websites such as LinkedIn, Glassdoor, and JobStreet have lots of publicly available data that are ripe for scraping, analysis, and some machine learning magic. I chose to look into JobStreet since it shows some salary estimates for the job postings and is also one of the most popular employment sites in Southeast Asia.</p><p><strong>The exact code used in this post will be valid for as long as the site does not change the tags or attributes that it uses from the time of writing.</strong> The transient nature of web scraping code is exactly the reason why I’ve decided to write my whole project process, from planning to coding. Because even if the copy-pasted codes may not work a year from now,<strong> the copy-pasted process used to write the same code will still be useful for the years and projects to come.</strong></p><p>Without further ado, let’s dive into the <strong>3 step web scraping process</strong>.</p><h3><strong>Step 1: Pre-preparation.</strong></h3><p>You’ll need the following before we begin:</p><p>· <strong>Selenium &amp; Web driver</strong>: Selenium 4 and ChromeDriver was used for this project since some information in the website are dynamically generated. Here are some links to get <a href="https://www.selenium.dev/documentation/webdriver/getting_started/install_library/">Selenium</a> &amp; <a href="https://www.selenium.dev/documentation/webdriver/getting_started/install_drivers/">WebDrivers.</a></p><p>· <strong>SelectorsHub</strong>: as you’ll see later on, this add-on is a godsend when web scraping! You can get Selector’s hub <a href="https://selectorshub.com/">here</a>.</p><p>· <strong>Jupiter Notebook</strong> — JobStreet will sometimes give you an “Are you a robot?” check atleast once while web scraping.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/763/1*5daejcOTe5jOe_6ev5uXIQ.png" /></figure><p>These checks are challenging to solve using code. The kernel-per-kernel execution nature of notebooks enables you to manually override these checks and then allow the scraper to continue scraping afterward.</p><p><strong>Bonus points: </strong>It’s not required, but some knowledge of HTML DOM can go a long way in making your scraping journey easier.</p><h3><strong>Step 2: Check out the website</strong></h3><p>Get a sense of how the code is supposed to flow. Visualizing the end-to-end flow can help you breakdown the process into its major phases. Scraping the search results of a website can often be broken down into 3 phases:</p><ul><li><strong>Phase 1: The Home Page</strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0cG8C4591VG0zGKh5Vcjbw.png" /><figcaption>We need to pull up the homepage, enter a keyword and location, and click search.</figcaption></figure><ul><li><strong>Phase 2: The Result Pages</strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bMpfbThqMp6tNTy_d-YlEg.png" /><figcaption>Each job post must first be clicked so it would display the job description for scraping.</figcaption></figure><ul><li><strong>Phase 3: Moving through pages until the last page</strong></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tHGU835Fg2PSMOE0O4Ixcw.png" /><figcaption>In the last page. the “Next” button is disabled. use this as your stopping point.</figcaption></figure><p>Now that you have an overview of what needs to be done, Its finally time to code!</p><h3><strong>Step 3: Write the code</strong></h3><p>import the necessary libraries.</p><pre><em>from</em> selenium <em>import</em> webdriver<br><em>from</em> selenium.webdriver.common.keys <em>import</em> Keys<br><em>from</em> selenium.webdriver.support <em>import</em> expected_conditions <em>as</em> EC<br><em>from</em> selenium.webdriver.support.expected_conditions <em>import</em> visibility_of_element_located</pre><pre><em>from</em> selenium.webdriver.chrome.service <em>import</em> Service<br><em>from</em> selenium.webdriver.chrome.options <em>import</em> Options<br><em>from</em> selenium.webdriver.common.by <em>import</em> By<br><em>from</em> selenium.webdriver.support.wait <em>import</em> WebDriverWait</pre><pre><em>from</em> datetime <em>import</em> datetime</pre><pre><em>import</em> pandas <em>as</em> pd<br><em>import</em> numpy <em>as</em> np<br><em>import</em> random<br><em>import</em> time<br><em>import</em> os<br><em>import</em> re</pre><p>Create a directory where you’ll save the scraped data.</p><pre>now = datetime.now()<br>start_date = now.strftime(&#39;%m%d%Y&#39;)</pre><pre># if you re-use the scraper on a different day, the code below will create a different folder for that day. so old files will not be overriden</pre><pre>os.makedirs(&#39;jobstreet/&#39; + start_date , <em>exist_ok</em>=True)<br></pre><p>Run selenium and pull up the home page.</p><pre>#this is where chromedriver is saved in my local machine<br>chrome_path = <em>r</em>&#39;C:/Users/user/scraper_sel/chromedriver.exe&#39;</pre><pre>service = Service(<em>executable_path</em>=chrome_path)<br>chrome_options = webdriver.ChromeOptions()</pre><pre>driver = webdriver.Chrome(<em>service</em>=service)<br>driver.implicitly_wait(8)<br>url = <em>r</em>&#39;http://www.jobstreet.com.ph/&#39;</pre><pre>driver.get(url)This is what I see when I run the script on my end</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sJk8s1nMtLm1Z-OV9gJXmg.png" /><figcaption>A new scraper is born! sorta.</figcaption></figure><h3><strong>Phase 1: The Home page</strong></h3><p>Now we’ll have to inspect the HTML code to see if there are any unique identifier for these input boxes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DrHHg0V2meoDi_fEAxE4fw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/708/1*fmZzWAqpThVFFVBRBSaVEQ.png" /></figure><p>Luckily, there is an <em>id=“searchKeywordsField”</em> for the keyword input box. IDs are great to use if they are available because one <em>id</em> can only be placed on one unique tag per page.</p><p>After doing the same <em>id</em> search process for the location input box, we can come up with these codes:</p><pre>keyword_field = driver.find_element(By.ID, &#39;searchKeywordsField&#39;)<br>location_field = driver.find_element(By.ID, &#39;locationAutoSuggest&#39;)</pre><pre># clear each input boxes before entering your preferred input to avoid placeholder texts messing up your search results</pre><pre>keyword_field.clear()<br>location_field.clear()</pre><pre>keyword = &#39;Data Science&#39;<br>location = &#39;Philippines&#39;</pre><pre>keyword_field.send_keys(keyword)<br>location_field.send_keys(location)</pre><p>Next, we’ll inspect the “Search” button.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Mbf0ZmlqngR6-sAU7rBL0g.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/731/1*UiOq59PTQcGOukDyP0iyYg.png" /></figure><p>This button does not have an <em>id, </em>the classes it has aren’t very unique, and frankly, the code is just messy to read . I often use XPATHs and SelectorsHub for these cases. You can quickly read up on XPATHs <a href="https://medium.com/@sunwrn/learn-xpath-in-5-minutes-a-tutorial-for-beginners-3a29925c9178">here </a>but you don’t have to master it to use. SelectorsHub can simply generate the XPATH for you to copy and paste into your code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/490/1*qqiMLsDX5-8Tm_6Edpt1dg.png" /><figcaption>SelectorsHub is God’s gift on earth.</figcaption></figure><pre>search_button = driver.find_element(By.XPATH, &quot;//button[@type=&#39;submit&#39;]&quot;)</pre><pre>search_button.click()</pre><p>With that, we are now in the results page where we’ll scrape the details for each job post.</p><h3><strong>Phase 2: The Result Pages</strong></h3><p>As we inspect the code, we can see that each job post is held within an <em>article</em> tag.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nVEWVvl-MDXr_CIRA_jJeg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/456/1*iG5Md5oS4Px5LzoFtdfcSA.png" /></figure><p>Using find_elements on the <em>article </em>tag<em> </em>allows us to have an object that we can iterate over. Each iteration requires us to first click the job post so it will display the full job description for scraping.</p><pre>articles = driver.find_elements(By.TAG_NAME, &quot;article&quot;)</pre><pre><em>for</em> article <em>in</em> articles:</pre><pre>job_button = WebDriverWait(driver, 20).until(EC.element_to_be_clickable(article)</pre><pre>driver.execute_script(&#39;arguments[0].click();&#39;, job_button)</pre><pre># With the full job description displayed, we can now start scraping</pre><p>Our goal is to get the following data for each job post:</p><ul><li><strong>Job title</strong></li><li><strong>Job location</strong></li><li><strong>Company name</strong></li><li><strong>Salary</strong></li><li><strong>Job description</strong></li><li><strong>URL of the job post</strong></li></ul><p>within each article we can see that the information we need are located within in different <em>span</em> articles.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1018/1*TxRoVPIU2uh6Dlk-8WU1eQ.png" /></figure><p>you can manually count the index numbers of these spans to get the info we need or you can use the <strong>console</strong> to speed things up.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/598/1*41_v0ppdYQMa11wv0DzVBw.png" /><figcaption>The console is just beside the elements tab</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/599/1*Tie4OXTtcK4h1bjb0_6bmA.png" /><figcaption>get rid of the scary looking items with clear()</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/742/1*dJ5xV9nttA1lYrsAlu4XiA.png" /><figcaption>The console found 30 article tags that we can index over</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/748/1*TkFfW9b9ER0LVAXnCy_kYw.png" /><figcaption>within the 1st job post (0th index) are 12 span tags</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/745/1*t3t9O8Pq4c5k5qcpJ-sP4A.png" /><figcaption>hovering over the index of each span will show it’s location on page (notice a faint highlight on index 0).</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/652/1*jOZS5Mme8Jwu2fD4P-srJA.png" /><figcaption>The Job title can be found on the 0th span index.</figcaption></figure><p>Identifying the index of each span will now allow us to write the code for all the details we need.</p><pre>page = 1</pre><pre>to_frame = []</pre><pre>articles = driver.find_elements(By.TAG_NAME, &quot;article&quot;)</pre><pre><em>for</em> article <em>in</em> articles:</pre><pre>job_button = WebDriverWait(driver, 20).until(EC.element_to_be_clickable(article)</pre><pre>driver.execute_script(&#39;arguments[0].click();&#39;, job_button)</pre><pre>title = article.find_elements(By.TAG_NAME, &#39;span&#39;)[0].text.strip()<br>company = articlefind_elements(By.TAG_NAME, &#39;span&#39;)[1].text.strip()<br>location = article.find_elements(By.TAG_NAME, &#39;span&#39;)[3].text.strip()</pre><pre># not all job posts will have salary info<br># if the text captured does not have a php sign, then we leave it blank</pre><pre>test_salary = article.find_elements(By.TAG_NAME, &quot;span&quot;)[5].text.strip()</pre><pre><em>if</em> re.search(&#39;php&#39;, test_salary.lower()):<br>salary = test_salary</pre><pre><em>else</em>:<br>salary = None</pre><pre># Since there&#39;s only one job description on screeen at a time<br># an XPATH can be used to locate this info</pre><pre>summary = driver.find_element(By.XPATH, &quot;//div[@data-automation=&#39;jobDescription&#39;]&quot;).text.strip()</pre><pre>job_url = article.find_elements(By.TAG_NAME, &#39;a&#39;)[0].get_attribute(&#39;href&#39;)</pre><pre>job_info = {<br>&#39;location&#39;: location,<br>&#39;title&#39;: title,<br>&#39;company&#39;: company,<br>&#39;salary&#39;: salary,<br>&#39;summary&#39;: summary,<br>&#39;job_url&#39;: job_url}</pre><pre>to_frame.append(job_info)</pre><p>Now it’s just a matter of of looping over every <em>article</em> in the page. you may also want to save your progress before moving to the next page.</p><pre>framed_df = pd.DataFrame(to_frame)</pre><pre>framed_df.to_csv(&#39;jobstreet/&#39; + start_date + &#39;/page&#39; + str(page) + &#39;.csv&#39;, <em>index</em>=False)</pre><h3>Phase 3: <strong>Moving through pages until the last page</strong></h3><p>We’re almost done! all we have to do now is to find out what makes the last page different from all other pages. For JobStreet, it can be seen on the “Next” button.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/672/1*o8U6LhSRwykT1LqaUnlxNg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/497/1*ughBoj6J4kih8n-ZJveEgw.png" /></figure><p>The “Next” button for the last page is disabled. <strong>So even though that button can still be clicked by Selenium, the url on the page will not change</strong>. We can use this as a signal to stop scraping.</p><pre>next_page = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH,&quot;//span[normalize-space()=&#39;Next&#39;]&quot;)))</pre><pre>driver.execute_script(&quot;arguments[0].click();&quot;, next_page)</pre><pre>next_url = driver.current_url</pre><pre><em>if</em> start_url <em>==</em> next_url:</pre><pre>print(<em>f</em>&quot;Scraper stopped moving, scaping ended at page {page}&quot;)</pre><pre><em>break </em></pre><pre>else:</pre><pre>page += 1 #continue scraping to the next page</pre><p>Now we have a scraper that is coded to stop at the end (without any nasty error messages). The fruit of your scraping labor per page will look like the one below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ypm81awpj0Y3Mj90AJdDKQ.png" /><figcaption>Data Cleaning is a topic for another day.</figcaption></figure><p><strong>In Conclusion</strong></p><p>Web scraping is not the easiest skill to learn. A mentor of mine called it <em>“resource exhausting” </em>as it<strong> </strong>can exhaust your time, your tears, and whatever resource it is that you run on that kinda makes you a pleasant person to talk to. But despite all that, <strong>Web scraping is a great skill to have. </strong>Because instead of having only a small sample of information that can bias your overall view, you can create bots that can help you scape all these pieces of information and put it together to help you make sense of the bigger picture.</p><p>However, if the notion of viewing a less-biased world is not a appeal to you, then just think of this: <strong>When you write a webscraping code, you’re literally making bots! </strong>maybe not <em>literally</em> but it’s still pretty cool, right?</p><p><strong>Grab the full code on </strong><a href="https://github.com/leecvz/leecvz_portfolio/tree/main/Project_JobStreet"><strong>GitHub</strong></a><strong>.</strong></p><p>Codes for scraping Glassdoor and Jobstreet can also be found there as well.</p><p>This post is <strong>Part 1 of 3</strong> for <strong>The JobStreet Project</strong>. Part 2 will be about Analytics and Part 3 will be about Artificial Intelligence.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1791400a7513" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>