Building a Web Crawler Using Selenium and Proxies
Tags: crawl, selenium, proxy
Published: 2021-06-12 06:49:10

<p>Once upon a time, people looking for information had to physically walk into a brick-and-mortar library, find the right books, and read through them intently.</p>
<p>Today, it seems a given that whatever data you&rsquo;re looking for exists on the Internet. There are over a billion websites on the World Wide Web at any given moment, containing enough information to take up out 305 billion printed sheets of paper.</p>
<p>The good news is that no matter what kind of data you&rsquo;re looking for, you can be sure to find it online. The bad news is that there is so much data online that personally sifting through it borders on the physically impossible. <img class="aligncenter size-full wp-image-379" src="https://www.bestproxyreviews.com/wp-content/uploads/2019/03/World-Wide-Web.jpg" alt="World Wide Web" width="800" height="494" /> Add in the fact that most websites have different scopes, formats, and frameworks. About 30% of websites use <a href="https://venturebeat.com/2018/03/05/wordpress-now-powers-30-of-websites/" target="_blank" rel="noopener noreferrer">WordPress</a>, for instance, and the rest use a variety of other platforms like Joomla, Drupal, Magento, etc.</p>
<p>Enter web crawling. Web crawlers are automated data-gathering tools that interact with websites on their owners&rsquo; behalf. This lets you access reams of data ready for output to a local database or spreadsheet for further analysis.</p>
<p>Although it may sound complicated, the truth is that building a web crawler using <a href="https://selenium.dev/" target="_blank" rel="noopener noreferrer">Selenium</a> is a pretty straightforward process. Let&rsquo;s dive in and find out exactly what you need to get started. <img class="aligncenter size-full wp-image-376" src="https://www.bestproxyreviews.com/wp-content/uploads/2019/03/Two-Ways-to-Crawl-Web-Data.jpg" alt="Two Ways to Crawl Web Data" width="1063" height="531" /></p>
<hr />
<h2 style="text-align: center;">There are Two Ways to Crawl Web Data</h2>
<p>One of the first obstacles you&rsquo;ll encounter when learning how to build a web crawler using Selenium is the fact that websites don&rsquo;t seem to like it. Web crawlers generate a lot of traﬃc, and website administrators tend to feel like web crawlers abuse the server resources they make available to the public.</p>
<p>But major Internet companies like Google crawl data all the time. The only diﬀerence is that they ask permission and offer something in return (in Google&rsquo;s case, placement on the world&rsquo;s number-one search engine). What do you do if you need access to data and don&rsquo;t have the convenient backing of a powerful economic incentive on your side?</p>
<p>You can use Selenium to collect data from websites through a browser &ndash; just like a regular user would. But since web administrators don&rsquo;t like it, you&rsquo;ll need the <a href="https://www.varonis.com/blog/what-is-a-proxy-server/">proxy</a> to hide your identity behind so that they can&rsquo;t trace the activity back to you.</p>
<p>Depending on your jurisdiction and the jurisdiction of the website you want to access, using a proxy could be a life-saver. In 2011, <a href="https://www.theglobeandmail.com/report-on-business/industry-news/the-law-page/why-reading-a-websites-fine-print-matters/article595795/">a court in British Columbia</a> punished a company for scraping content from a real estate website, but more recent cases allow crawling of publicly accessible content.</p>
<p>Journalists, data analysts, and programmers generally don&rsquo;t have the resources Google brings to the table when it asks for web crawler access.</p>
<hr />
<h2>Selenium &ndash; How it Works and Why You Should Use It</h2>
<p>There are lots of tools and platforms you can use to scrape web data, but most have limitations. For instance, if you use the <a href="https://www.makeuseof.com/tag/build-basic-web-crawler-pull-information-website-2/">Python module Scrapy</a>, you can only access websites that don&rsquo;t feature JavaScript-heavy user interfaces.</p>
<p>Selenium is a simple tool for automating browsers. With Selenium, you can automate a web browser like Google Chrome or Safari so that any website is crawl-compatible.</p>
<p>The first step is downloading and setting up Selenium. You will need to download a version of Selenium specifically tailored to your browser. For Google Chrome, for instance, this is called <a href="https://sites.google.com/a/chromium.org/chromedriver/downloads" target="_blank" rel="noopener noreferrer">ChromeDriver</a>.</p>
<p>When you extract the file (ChromeDriver.exe, for instance), make sure to remember where you put it, because you&rsquo;ll need it later.</p>
<p>In order to use Selenium to build a web crawler, you&rsquo;ll need some extra Java modules. This requires a little bit of coding, but it&rsquo;s not that complicated. First, install <a href="https://spring.io/guides/gs/maven/" target="_blank" rel="noopener noreferrer">Maven</a>, which is what you&rsquo;re going to use to build the Java program. <img class="aligncenter size-full wp-image-377" src="https://www.bestproxyreviews.com/wp-content/uploads/2019/03/Use-to-build-the-Java-program.jpg" alt="Use to build the Java program" width="1063" height="602" /> Once Maven is ready, you must add this dependency to POM.xml: Now just run the build process and you&rsquo;re ready to take your first steps with Selenium.</p>
<hr />
<h2 style="text-align: center;"><strong>Basic Introduction to Using Selenium</strong></h2>
<p><img class="aligncenter size-full wp-image-373" src="https://www.bestproxyreviews.com/wp-content/uploads/2019/03/Basic-Introduction-to-Using-Selenium.jpg" alt="Basic Introduction to Using Selenium" width="1058" height="607" /> Let&rsquo;s start with something simple. First, create an instance of ChromeDriver:</p>
<blockquote>WebDriver driver = new ChromeDriver();</blockquote>
<p>Now you&rsquo;ll have a Google Chrome window open. To navigate to a web page, use this command (using example.com as an example):</p>
<blockquote>driver.get("http://www.example.com");</blockquote>
<p>To locate HTML elements on a page, use <strong>WebDriver.findElement()</strong>. To get the page title, your command should look like this:</p>
<blockquote>System.out.println("Title: " + driver.getTitle());</blockquote>
<p>This is how Selenium works. It assigns a coding matrix to the browser so that you can automate the things you would normally do by hand. It&rsquo;s a simple, and powerful way to complete a broad variety of time-intensive tasks. To close out the session, use this command: driver.quit();</p>
<p>And that&rsquo;s it. You&rsquo;ve successfully controlled a browser session using Java in Selenium. To learn more about using Selenium as a web crawler, use this <a href="https://github.com/TheDancerCodes/Selenium-Webscraping-Example" target="_blank" rel="noopener noreferrer">GitHub tutorial</a>. Once you know the commands and understand the methodology, the entire Internet is open to you.</p>
<hr />
<h2>Proxies &ndash; What to Look for When Building a Web Crawler Using Selenium</h2>
<p>When using Selenium to scrape websites, the main thing you want to protect yourself against is blacklisting. Since web administrators will generally automatically treat Selenium-powered web crawlers as threats, you need to protect your web crawler.</p>
<p>Nobody can guarantee that your web scraper will never get blacklisted, but choosing the right proxy can make a big difference and improve the life expectancy of your crawler. <img class="aligncenter size-full wp-image-374" src="https://www.bestproxyreviews.com/wp-content/uploads/2019/03/Block-web-crawlers-based-on-the-IP-address.jpg" alt="Block web crawlers based on the IP address" width="1059" height="542" /> The majority of websites will block web crawlers based on the IP address of the originating server or the user&rsquo;s hosting provider. Clever web administrators will use intelligent tools to determine the pattern of a certain pool of IP addresses and then block the whole bunch.</p>
<p>What you need is a proxy that can shift between multiple IP addresses. Don&rsquo;t settle for a simple solution, either:</p>
<ul>
<li>Some experts recommend using between 50 and 100 distinct IP addresses to be sure you have a large enough pool.</li>
</ul>
<p><sup>o</sup> Make sure you don&rsquo;t get consecutive IP addresses (1.2.3.4 to 1.2.3.5 to 1.2.3.6, for example).</p>
<p>You need randomized IP addresses with no logical correlation between them.</p>
<p>The important thing is that Selenium, by its nature, is powerfully customizable. Your imagination and coding skills are the only limit to your ability to build a web crawler using Selenium.</p>
<p>For instance, if you are using the Requests library (more information <a href="https://pypi.org/project/selenium-requests/" target="_blank" rel="noopener noreferrer">here</a> ) then you can write code to use proxy IPs with Selenium like so:</p>
<blockquote>r = requests.get('example.com',headers=headers,proxies={'https': proxy_url}) proxy = get_random_proxy().replace('\n', '') service_args = [ '--proxy={0}'.format(proxy), '--proxy-type=http', '--proxy-auth=user:password' ] print('Processing..' + url) driver = webdriver.PhantomJS(service_args=service_args)</blockquote>
<p>Where <strong>example.com</strong> is the website you would like to access and <strong>get_random_proxy</strong> is the command to obtain a random proxy from within your pool. <img class="aligncenter size-full wp-image-375" src="https://www.bestproxyreviews.com/wp-content/uploads/2019/03/Import-of-Selenium-Web-Driver.jpg" alt="Import of Selenium Web Driver" width="1064" height="484" /></p>
<ul>
<li><a href="https://www.bestproxyreviews.com/selenium-proxy/">Selenium Proxy Setting - How to Setup Proxies on Selenium</a></li>
</ul>
<p>But this is just the beginning of integrating proxies with your Selenium web crawler. There&rsquo;s much more you can do:</p>
<ul>
<li>You can program Selenium to implement a system that sets the frequency of an IP address visiting a target website per day or per hour and then disables that IP address for 24 hours once it reaches its limit.</li>
<li>You can set Selenium to record the IP addresses that get blacklisted. This lets you streamline the process of requesting new IP addresses because you only need to replace the ones that are blocked.</li>
<li>You can increase Selenium&rsquo;s page-load waiting time to adjust for timeouts. If you are overtaxing the target server and using proxies, you may need to adjust page-load wait times to make Selenium more patient. Investing in a higher quality proxy can ensure faster response times.</li>
</ul>
<p>Now there are <a href="https://www.privateproxyreviews.com/rotating-proxies/">lots of rotating proxy services in the market</a>, The rotating proxy work as "backconnect" that offer proxy API to rotate the IP addresses automatically, if you use those type of services, that will save lots of time on proxies setting up.&nbsp;</p>
<p>With a powerful tool like Selenium supported by top-shelf proxies that you can rely on, you will be able to seamlessly gather data from anywhere on the Internet without exposing any vulnerabilities. Enjoy and happy crawling!</p>