Web Scraping Using Selenium and Python: The Step-By-Step Guide for Beginner
Published: 2021-04-05 07:06:36

<blockquote>For dynamic sites richly built with JavaScript, Selenium is the tool of choice for extracting data from them. Come in now and read this article to learn how to extract data from web pages using Selenium.</blockquote>
<p><img class="aligncenter size-full wp-image-7290" src="https://www.bestproxyreviews.com/wp-content/uploads/2020/12/Web-Scraping-Using-Selenium-and-Python.jpg" alt="Web Scraping Using Selenium and Python" width="1000" height="555" /> The easiest websites to scrape data from are static pages that all content is downloaded upon request. Sadly, these types of sites are gradually fading out, and dynamic websites are gradually taking over.</p>
<p>With dynamic sites, all content on a page is not provided upon loading a page &ndash; the content is dynamically added after specific JavaScript events, which poses a different problem to scraping tools designed for static websites. Fortunately enough, with tools like <a href="https://www.selenium.dev/projects/" target="_blank" rel="noopener noreferrer">Selenium</a>, you are able to trigger JavaScript events and scrape any page you want, no matter how JavaScript-rich a page is.</p>
<p>With Selenium, you are not tied to a single language like other tools. Selenium has support for Python, Ruby, Java, C#, and JavaScript. In this article, we will be making use of Selenium and Python to extract web data. Before we go into that in detail, it is wise if we look at Selenium and instances when you should make use of it.</p>
<hr />
<h2 style="text-align: center;"><strong>Selenium WebDriver &ndash; an Overview </strong></h2>
<hr />
<p>Selenium was not initially developed for web scraping &ndash; it was initially developed for testing web applications but has found its usage in web scraping. In technical terms, Selenium or, more appropriately, Selenium WebDriver is a portable framework for testing web applications.</p>
<p>In simple terms, all Selenium does is automate web browsers. And as the team behind Selenium rightfully put it, what you do with that power is up to you! Selenium has support for Windows, macOS, and Linux. In terms of browser support, you can use it for automating Chrome, Firefox, Internet Explorer, Edge, and Safari. Also important is the fact that Selenium can be extended using third-party plugins. <img class="aligncenter size-full wp-image-7293" src="https://www.bestproxyreviews.com/wp-content/uploads/2020/12/Selenium-WebDriver.jpg" alt="Selenium WebDriver" width="1000" height="430" /> With Selenium, you can automate the filling of forms, clicking buttons, taking a snapshot of a page, and other specific tasks online. One of these tasks is web extraction. While you can use it for web scraping, it is certainly not a Swiss Army Knife of web scraping; it has its own downside that will make you avoid using it for certain use cases.</p>
<p>The most notable of its downsides is its slow speed. If you have tried using Scrapy or the combo of <a href="https://pypi.org/project/requests/" target="_blank" rel="noopener noreferrer">Requests</a> and <a href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/" target="_blank" rel="noopener noreferrer">Beautifulsoup</a>, you will have a speed benchmark that will get you to rank Selenium slow. This is not unconnected to the fact that it makes use of a real browser, and rendering has to take place.</p>
<p>For this reason, developers only use Selenium when dealing with JavaScript-rich sites that you will find it difficult to call underlying APIs. With Selenium, all you do is automate the process, and all events will be triggered. For static sites that you can quickly replicate API requests, and all content is downloaded upon loading, you will want to use the better option, which is Scrapy or the duo of Requests and Beautifulsoup.</p>
<hr />
<h2 style="text-align: center;"><strong>Installation Guide</strong></h2>
<hr />
<p><img class="aligncenter size-full wp-image-7296" src="https://www.bestproxyreviews.com/wp-content/uploads/2020/12/Selenium-Installation-Guide.jpg" alt="Selenium Installation Guide" width="942" height="546" /> Selenium is a third-party library, and as such, you will need to install it before you can make use of it. Before installing Selenium, make sure you already have Python installed. To install Python, you can <a href="https://www.python.org/downloads/" target="_blank" rel="noopener noreferrer">visit the Python official download page</a>. For Selenium to work, you will need to install the Selenium package and then the specific browser driver you want to automate. You can install the library using pip.</p>
<pre>pip install Selenium</pre>
<p>For browser drivers, they have support for Chrome, Firefox, and many others. Our focus in this article is on Chrome. If you don&rsquo;t have Chrome installed on your computer, you can <a href="https://www.google.com/chrome/" target="_blank" rel="noopener noreferrer">download it from the official Google Chrome page</a>. With Chrome installed, you can then go ahead and <a href="https://sites.google.com/a/chromium.org/chromedriver/downloads" target="_blank" rel="noopener noreferrer">download the Chrome driver binary here</a>.</p>
<p>Make you download the driver for the version of Chrome you have installed. The file is a zip file with the actual driver inside of it. Extract the actual Chrome driver (chromedriver.exe) and place it in the same folder as any Selenium script you are writing.</p>
<hr />
<h2 style="text-align: center;"><strong>Selenium Hello World </strong></h2>
<hr />
<p>As it is in the coding tutorial tradition, we are starting this selenium guide with the classical hello world program. The code does not scrape any data at this point. All it does is attempt to log into an imaginary Twitter account. Let take a look at the code below.</p>
<pre>import time
from selenium import webdriver
from selenium.webdriver.common.keysimport Keys

username = "concanated"
password = "djhhfhfhjdghsd"
driver = webdriver.Chrome()
driver.get("https://twitter.com/login")
name_form = driver.find_element_by_name("session[username_or_email]")
name_form.send_keys(username)
pass_form = driver.find_element_by_name(("session[password]"))
pass_form.send_keys(password)
pass_form.send_keys((Keys.RETURN))
time.sleep(5)
driver.quit()</pre>
<p>the username and password variables&rsquo; values are dummies. When you run the above code, it will launch Chrome and then open the Twitter log in page. The username and password will be inputted and then sent.</p>
<p>Because the username and password are not correct, it displays an error message, and after 5 seconds, the browser is closed. As you can see from the above, you need to specify the specific web browser, and you can see we did that on line 7. The get method sends GET requests. After the page has loaded successfully, we use the</p>
<pre>driver.find_element_by_name</pre>
<p>method to find the username and input elements and then use</p>
<pre>.send_keys</pre>
<p>for filling the input fields with the appropriate data.</p>
<hr />
<h2 style="text-align: center;"><strong>Sending Web Requests </strong></h2>
<hr />
<p>Sending web requests using Selenium is one of the easiest tasks to do. Unlike in the case of other tools that differentials between POST and GET requests, in Selenium, they are sent the same way. All that&rsquo;s required is for you to call the get method on the driver passing the URL as an argument. Let see how that is done in action below.</p>
<pre>from selenium import webdriver

driver = webdriver.Chrome()
# visit Twitter homepage
driver.get("https://twitter.com/")
# page source
print(driver.page_source)
driver.quit()</pre>
<p>Running the code above will launch Chrome in automation mode and visit the Twitter homepage and print the HTML source code of the page using the</p>
<pre>driver.page_source</pre>
<p>. You will see a notification below the address bar telling you Chrome is being controlled by an automated test software.</p>
<hr />
<h2 style="text-align: center;"><strong>Chrome in Headless Mode</strong></h2>
<hr />
<p>From the above, Chrome gets launched &ndash; this is the headful approach and used mainly for debugging. If you are ready to launch your script on a server or in a production environment, you wouldn&rsquo;t want Chrome launched &ndash; you will want it to work in the background. This method of running the Chrome browser without it launching is known as the headless Chrome mode. Below is how to run Selenium Chrome in headless mode.</p>
<pre>from selenium import webdriver
from selenium.webdriver.chrome.optionsimport Options

# Pay attention to the code below
options = Options()
options.headless = True
driver = webdriver.Chrome(options=options)

# visit Twitter homepage
driver.get("https://twitter.com/")
# page source
print(driver.page_source)
driver.quit()</pre>
<p>Running the code above will not launch Chrome for you to see &ndash; all you see is the source code of the page visited. The only difference between this code and the one before it is that this one is running in the headless mode.</p>
<hr />
<h2 style="text-align: center;"><strong>Accessing Elements on a Page</strong></h2>
<hr />
<p>There are basically 3 things involved in web scraping &ndash; sending web requests, parsing page source, and then processing or saving the parsed data. The first two are usually the focus as they present more challenges.</p>
<p>You have already learned how to send web requests. Now let me show you how to access elements in other to parse out data from them or carry out a task with them. In the code above, we use the</p>
<pre>page_source</pre>
<p>method to access the page source. This is only useful when you want to parse using Beautifulsoup or other parsing libraries. If you want to use Selenium, you do not have to use the</p>
<pre>page_source</pre>
<p>method. [su_list icon="icon: hand-o-right" icon_color="#0E86D4"]Below are the options available to you.</p>
<ul>
<li>
<pre>driver.title</pre>
is for retrieving page title</li>
<li>
<pre>driver.current_url</pre>
for retrieving the URL of the page in view.</li>
<li>
<pre>driver.find_element_by_name</pre>
for retrieving an element by its name, e.g., password input with name password.</li>
<li>
<pre>driver.find_element_by_tag_name</pre>
for retrieving element by tag name such as a, div, span, body, h1, etc.</li>
<li>
<pre>driver.find_element_by_class_name</pre>
for retrieving element by class name</li>
<li>
<pre>driver.find_element_by_id</pre>
for finding element by id.</li>
</ul>
<p>For each of the</p>
<pre>find_element_by***</pre>
<p>methods, there is a corresponding method that retrieves a list of elements instead of one except for</p>
<pre>find_element_by_id</pre>
<p>. Take, for instance, if you want to retrieve all elements with the &ldquo;thin-long&rdquo; class, you can make use of the</p>
<pre>driver.find_elements_by_class_name(&ldquo;thin-long&rdquo;)</pre>
<p>instead of</p>
<pre>driver.find_element_by_class_name(&ldquo;thin-long&rdquo;)</pre>
<p>. The difference is the plurality of the element keyword in the function.</p>
<hr />
<h2 style="text-align: center;"><strong>Interacting with Elements on a Page</strong></h2>
<hr />
<p>With the above, you can find specific elements on a page. However, you do not just do that for doing sake; you will need to interact with them either to trigger certain events or retrieve data from them. Let take a look at some of the interactions you can have with elements on a page using Selenium and Python.</p>
<ul>
<li>
<pre>element.text</pre>
will retrieve the text attached to an element</li>
<li>
<pre>element.click()</pre>
will trigger the click action and events that follow that</li>
<li>
<pre>element.send_keys(&ldquo;test text&rdquo;)</pre>
is meant for filling input forms</li>
<li>
<pre>element.is_displayed()</pre>
is for detecting if an element is visible to real users or not -this is perfect for honeypot detection.</li>
<li>
<pre>element.get_attributes(&ldquo;class&rdquo;)</pre>
for retrieving the value of an element&rsquo;s attribute. You can change the &ldquo;class&rdquo; keyword for any other attribute.[/su_list]</li>
</ul>
<p>With the above, you have what is required to start scraping data from web pages. I will be using the above to scrape the <a href="https://www.britannica.com/topic/list-of-state-capitals-in-the-United-States-2119210" target="_blank" rel="noopener noreferrer">list of US states their capital, population (census), and estimated population from the Britannica website</a>. Take a look at the code below.</p>
<pre>from selenium import webdriver
from selenium.webdriver.chrome.optionsimport Options

# Pay attention to the code below
options = Options()
options.headless = True
driver = webdriver.Chrome(options=options)

driver.get("https://www.britannica.com/topic/list-of-state-capitals-in-the-United-States-2119210")
list_states = []
trs = driver.find_element_by_tag_name("tbody").find_elements_by_tag_name("tr")
for iin trs:
tr = i.find_elements_by_tag_name("td")
tr_data = []
for x in tr:
tr_data.append(x.text)
list_states.append(tr_data)
print(list_states)
driver.quit()</pre>
<p>Looking at the above, we put into practice almost all of what we discussed above. Pay attention to the trs variable. If you look at the source code of the page, you will discover that the list of states and the associated information are contained in a table. The table does not have a class neither does its body.</p>
<p>Interestingly, it is the only table, and as such, we can use the find.element_by_tag_name(&ldquo;tbody&rdquo;) method to retrieve the tbody element. Each row in the tbody element represents a state and its information, each embedded in a td element. we called the find.elements_by_tag_name(&ldquo;td&rdquo;) to retrieve the td elements.</p>
<p>The first loop is for iterating through the tr elements. The second one is for iterating through the td elements for each of the tr elements. Element.text was used for retrieving text attached to an element.</p>
<hr />
<h2 style="text-align: center;"><strong>You Have Learnt the Basics: Now What?</strong></h2>
<p>From the above, we have been able to show you how to scrape a page using Selenium and Python. However, you need to know that what you have learned is just the basics. There is more you need to learn. You will need to know how to carry out other moves and keyboard actions.</p>
<p>Sometimes, just filling out a form with a text string at once will reveal traffic is bot-originating. In instances like that, you will have to mimic typing by filling in each letter one after the other. With Selenium, you can even take a snapshot of a page, execute custom JavaScript, and carry out a lot of automation tasks. I will advise you to learn more about the Selenium web browser on the <a href="https://www.selenium.dev/documentation/en/webdriver/" target="_blank" rel="noopener noreferrer">official Selenium website</a>.</p>
<hr />
<p><br /><br /></p>
<pre style="text-align: center;"><strong>Conclusion</strong></pre>
<p>Selenium has its own setback in terms of slow speed. However, it has proven to be the best option when you need to scrape data from a rich JavaScript website.</p>
<p>One thing you will come to like about Selenium is that it makes the whole process of scraping easy as you do not have to deal with cookies and replicating hard-to-replicate web requests. Interestingly, it is easy to make use of.</p>
<p>Source,&nbsp;https://www.bestproxyreviews.com/selenium-web-scraping-python/&nbsp;</p>