<?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 Sadiquetimileyin on Medium]]></title>
        <description><![CDATA[Stories by Sadiquetimileyin on Medium]]></description>
        <link>https://medium.com/@sadiquetimileyin?source=rss-59494754ecaf------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*RbseLKLJTA1KoTo4</url>
            <title>Stories by Sadiquetimileyin on Medium</title>
            <link>https://medium.com/@sadiquetimileyin?source=rss-59494754ecaf------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 22:45:44 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sadiquetimileyin/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[The Art of Web Scraping in Python using Selenium]]></title>
            <link>https://medium.com/data-epic/the-art-of-web-scraping-in-python-using-selenium-1c52901c34cc?source=rss-59494754ecaf------2</link>
            <guid isPermaLink="false">https://medium.com/p/1c52901c34cc</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[selenium]]></category>
            <category><![CDATA[web-scraping-tools]]></category>
            <category><![CDATA[selenium-webdriver]]></category>
            <category><![CDATA[web-scraping]]></category>
            <dc:creator><![CDATA[Sadiquetimileyin]]></dc:creator>
            <pubDate>Tue, 05 Dec 2023 20:27:05 GMT</pubDate>
            <atom:updated>2023-12-05T20:27:05.361Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="Scraping" src="https://cdn-images-1.medium.com/max/480/1*5l8Ve6uWP1yse6aCxOsgKQ.gif" /><figcaption>Scraper</figcaption></figure><p>What is web scraping? Web scraping is the process of extracting data from websites. It is a very potent method that transforms data gathering and processing. Web scraping has become a vital tool for both consumers and corporations due to the abundance of internet data.</p><p>Selenium is a Python library that can be used to perform automated testing on web applications. It can simulate user inputs, such as entering content, tapping buttons, and scraping sites. Selenium is an open-source tool that works on all major operating systems and browsers.</p><h4>Why use Selenium for web scraping?</h4><p>Python offers a large number of modules and frameworks that make it simple to extract data from websites, which makes it a popular programming language for web scraping.</p><p>Using Python and Selenium for web scraping offers several advantages over other web scraping techniques:</p><ol><li><strong>Dynamic websites:</strong> JavaScript and other scripting languages are used to build dynamic web sites. When the website loads completely or the user interacts with it, these sites frequently have elements that are visible. Because Selenium can communicate with various components, it’s an effective tool for data scraping from dynamic websites.</li><li><strong>User interactions: </strong>Selenium is capable of simulating scrolling, clicks, and form submissions. This enables you to scrape websites like login forms that demand input from users.</li><li><strong>Debugging: </strong>You may step through the scraping process with Selenium in debug mode, seeing what the scraper is doing at each stage. When something goes wrong, you can use this to troubleshoot it.(reference <a href="https://nanonets.com/blog/web-scraping-with-selenium/">nanonets.com</a>)</li></ol><h3>Setting up selenium:</h3><p>First and foremost, install the selenium driver using pip either on your local terminal or directly in your IDE</p><pre>pip install Selenium</pre><p>then, we install a web-driver manager. The web-driver has a common problem of needing to manually update the web-driver according to the version of chrome your are using so in this article we will cover ways to automatically update the chrome driver. This will automatically download and install the appropriate WebDriver for you. To install web driver-manager, you can use the following command:</p><pre>pip install webdriver-manager</pre><p>Setup complete.</p><p>Moving forward, step by step setup for selenium</p><h4>Step 1: The imports</h4><p>The imports below is the basic import needed when initializing selenium. we will be focusing mainly on using Chrome browser</p><pre>from selenium import webdriver<br>from Selenium.webdriver.common.keys import Keys<br>from Selenium.webdriver.common.by import By</pre><h4>Step 2: Web-driver manager</h4><p>Setting up web-driver manager on Chrome browser use the following imports:</p><pre>from selenium.webdriver.chrome.service import Service as ChromeService<br>from webdriver_manager.chrome import ChromeDriverManager</pre><p>The code below has a function that auto update your chrome driver automatically regardless of which version of chrome you are using. Using the code below:</p><pre> drivers = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))</pre><h4>Step 3: Basic Selenium functions</h4><ol><li>The <strong>get()</strong> function, it is used to access the website after the link has been provided as shown below:</li></ol><pre>link = &quot;htpps://google.com&quot; # This is just a reference <br>driver.get(link)</pre><p>2. The <strong>find_element() </strong>and <strong>find_elements()</strong>. The difference between this two functions is the find_element() returns the first find on the webpage that has the selector e.g h1 the find_element returns the first h1 on the webpage. Meanwhile the find_elements() fetches all the h1’s on the page and we use a for loop to access the data gotten from the find_elements(). implementation below:</p><pre># For the find element<br>driver.find_element(By.TAG_NAME, value=&#39;h1&#39;)<br><br># For find elements<br>driver.find_elements(By.CSS_SELECTOR, value=&#39;h1&#39;)<br><br># Don&#39;t worry about the By selector if it looks like mystery now dont&#39;t worry we will cover it in this article</pre><p>This functions above is mainly the three functions you will need for basic selenium usage.</p><p>The For loop solution as stated above the for loop basically stores the output directly into a list.</p><pre># For links<br>links = driver.find_elements(By.CSS_SELECTOR, value=&#39;.directory-content-box-inner a&#39;)<br>linked = []<br>for link in links:<br>    linked.append(link.get_attribute(&#39;href&#39;))<br><br># For texts <br>element = driver.find_elements(By.TAG_NAME, value=&#39;h3&#39;)<br>elements = []<br>for txt in element:<br>    elements.append(txt.text)</pre><h4>Step 4: Navigating using the By selector</h4><p>The By selector consists of 8 major selector types which are: NAME, ID, CSS_SELECTOR, TAG_NAME, CLASS_NAME, XPATH, LINK_TEXT, PARTIAL_LINK_TEXT. The following are various methods to navigate through the website.</p><pre>&lt;input type=&quot;text&quot; name=&quot;passwd&quot; class=&quot;passwd-class&quot; id=&quot;passwd-id&quot; /&gt; # Example of a basic html code</pre><p><strong>By.ID:<br></strong> This method is used when the id of a html code is given as shown below.</p><pre>element = driver.find_element(By.ID, value=&quot;passwd-id&quot;)</pre><p><strong>By.NAME:</strong> <br>This method is used when the name is indicated in the html code as shown below</p><pre>element = driver.find_element(By.NAME, value=&quot;passwd&quot;)</pre><p><strong>By.CSS_SELECTOR:</strong> <br>This method requires using css tag names to search through a webpage to select a specific part of the code that may have similarities with the other parts of the code.</p><pre>element = driver.find_element(By.CSS_SELECTOR, value=&quot;input#passwd-id&quot;)</pre><p><strong>By. XPATH:</strong> <br>When using this method we can write the XPATH manually but we have to be very careful the best practice is to copy directly from your web inspect page the explanation of how XPATH works is <a href="https://www.guru99.com/xpath-selenium.html"><strong><em>here</em></strong></a></p><figure><img alt="Chrome web inspect page" src="https://cdn-images-1.medium.com/max/747/1*TOXagtMpACXXaj-HugT-mg.jpeg" /><figcaption>How to copy XPATH</figcaption></figure><pre>element = driver.find_element(By.XPATH, value=&quot;//input[@id=&#39;passwd-id&#39;]&quot;)</pre><p><strong>By.CLASS_NAME:</strong> <br>This method is selecting the part of the html code by the name of the class indicated as shown below.</p><pre>element = driver.find_element(By.CLASS_NAME, value=&quot;passwd-class&quot;)</pre><p><strong>By.TAG_NAME:</strong> <br>This method is selecting the part of the html code by the name of the tag as shown below. Examples of tag names are h1, a, e.t.c.</p><pre>element = dirver.find_elements(By.TAG_NAME, value=&quot;input&quot;)</pre><p><strong>By.LINK_TEXT:</strong> <br>This method is using the texts that are attached to anchor tags in html as shown in the image below.</p><figure><img alt="Selection by link txt" src="https://cdn-images-1.medium.com/max/737/1*4d-mejfDMUr4vJ_D7iwboA.jpeg" /><figcaption>Explanatory image for link text</figcaption></figure><pre>element = driver.find_element(By.LINK_TEXT, value=&quot;click here&quot;)</pre><p><strong>By.PARTIAL_TEXT:</strong> <br>This is also the same as above but this does not require the full name of the link text just a bit of it is enough for it to run</p><pre>element = driver.find_element(By.PARTIAL_LINK_TEXT, value=&quot;here&quot;)</pre><h4>Step 5: Viewing texts in python IDE</h4><p>This step is necessary incase we want to save any data gotten from the website in a file or just a list and so on we use the following to pick outputs from the website.</p><ol><li>The text keyword at the end of the element is used to allow python decipher the coded output directly from the website then convert it to a text understood by the user. Example is shown below.</li></ol><pre>element = driver.find_element(By.TAG_NAME, value=&#39;h1&#39;)<br>element_name = element.text</pre><p>2. The get attribute function is used to mostly print out links from an anchor tag using the “href” keyword.</p><pre>element = driver.find_element(By.TAG_NAME, value=&#39;a&#39;)<br>link = element.get_attribute(&#39;href&#39;)</pre><p>The code above will print out the links found in the anchor tags</p><h4>Step 6: Saving into a file</h4><p>Saving all the output into a file we can always use the csv file format preferably because it is easier to work with in any other aspect especially during data cleaning.</p><pre>    with open(&quot;Canada_companies.csv&quot;, &#39;a&#39;) as file:<br>        files = file.write(f&quot;{element_name},{link}\n&quot;)</pre><p>Just an addition, for faster loading times we add the headless option this make the web-driver not need to open the website instead it open is in the background and it does all the scraping for you without opening the webpage.</p><pre>def chrome_driver():<br>    option = webdriver.ChromeOptions()<br>    option.add_argument(&#39;--headless&#39;)<br>    drivers = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()), options=option)<br>    return drivers</pre><h4>Conclusion:</h4><p>To sum up, using Selenium for web scraping is an effective way to retrieve data from websites. It can save you a lot of time and effort by enabling you to automate the data collection process. With Selenium, you may interact with websites in a manner similar to that of a human user and retrieve the necessary data more quickly.</p><p>Thank you for reading, more to come!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1c52901c34cc" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-epic/the-art-of-web-scraping-in-python-using-selenium-1c52901c34cc">The Art of Web Scraping in Python using Selenium</a> was originally published in <a href="https://medium.com/data-epic">Data Epic</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>