<?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 Abdullah Ahmad Zarir on Medium]]></title>
        <description><![CDATA[Stories by Abdullah Ahmad Zarir on Medium]]></description>
        <link>https://medium.com/@abdullahzarir?source=rss-beb8a22c56cf------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*E3yOEqomYGYY9uTmJAQ4jw.jpeg</url>
            <title>Stories by Abdullah Ahmad Zarir on Medium</title>
            <link>https://medium.com/@abdullahzarir?source=rss-beb8a22c56cf------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 21:08:42 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@abdullahzarir/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[Crawling in Parallel with Anonymous IP]]></title>
            <link>https://medium.com/@abdullahzarir/crawling-in-parallel-with-anonymous-ip-645c252bd433?source=rss-beb8a22c56cf------2</link>
            <guid isPermaLink="false">https://medium.com/p/645c252bd433</guid>
            <category><![CDATA[web-crawling]]></category>
            <category><![CDATA[tor]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[parallel-processing]]></category>
            <dc:creator><![CDATA[Abdullah Ahmad Zarir]]></dc:creator>
            <pubDate>Sat, 05 Jan 2019 09:00:32 GMT</pubDate>
            <atom:updated>2019-01-06T08:17:18.693Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1XUDUWYStv6bkv1AvegIyg.png" /><figcaption>Trace tree of the definition of World Wide Web from Wikipedia, generated with <a href="https://github.com/zargit/rabbit-hole">rabbit-hole</a>.</figcaption></figure><p>We are going to see how to crawl in parallel with anonymous IP addresses utilizing the tor relay network. This comes in handy when you need to crawl a huge amount of data from the same source within a very short time. Using multiple IP addresses helps to avoid getting blocked when you perform too many requests using the same IP.</p><p>We will write the script in python. First, let’s install all the dependencies. We have to install the tor executable from <a href="https://www.torproject.org/download/download-easy.html">here</a>. In Windows, we have to include the tor.exe in the environment path, for Ubuntu we can simply execute -</p><p>sudo apt-get install tor</p><p>Following that we need to install the python dependencies. We will install <a href="https://pypi.org/project/stem/">stem</a>, it is a python wrapper for communicating with the tor network.</p><p>pip install stem</p><p>Then we will install torrequest, a very simple wrapper on top of stem to easily perform http requests.</p><p>pip install torrequest</p><p>For parsing the fetched HTML content we will use the <a href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/#beautiful-soup-documentation">BeautifulSoup</a> package.</p><p>pip install beautifulsoup4</p><p>For this tutorial, we will crawl from Wikipedia. It is important that in your case wherever you wish to crawl from you are abiding by their terms and conditions, not violating any data policies. We are going to fetch the first sentence of a Wikipedia article, which is almost always the definition, we will crawl down other links mentioned in the definition. Now, let’s jump into the code and we will then go through it in details.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/56ab4211254e96f441564a87502ddcad/href">https://medium.com/media/56ab4211254e96f441564a87502ddcad/href</a></iframe><p>In the crawl() function we pass four arguments, the starting point for the crawling, the number of processes to spawn, depth is the stopping condition for the crawler and lastly the minimum port number, from where it should start looking for free ports. For each tor connection, we need two ports.</p><p>We need a <a href="https://docs.python.org/3/library/multiprocessing.html#managers">process manager</a> to have access to a shared space or data structure from all the processes. task_queue and result_dict both are shared data structures. task_queue holds all the tasks yet to be processed by any of the worker processes. We put the first article in the queue with depth 0.</p><p>Then inside the loop, we fetch the free ports and start all the worker processes. Let’s look at the Worker class.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3a4d0cae6c83c098aff4179067017053/href">https://medium.com/media/3a4d0cae6c83c098aff4179067017053/href</a></iframe><p>Inside the run() function, which is invoked when a new process is spawned, we first launch the tor connection. We use the provided ports and create a temporary folder for each tor connection in the users home directory to store tor connection data. Then we create a TorRequest object at the given ports. Establishing the tor connections takes a bit of time.</p><p>The while loop runs as long as there are items in the shared task_queue. The get() method of the queue is blocking, means it blocks until there is an item available in the queue, or we can specify how long it should wait with the timeout argument.</p><p>Following that we check the level of the current fetched item from the queue, if it is below the depth then we proceed with the tor http request. We simply use the get()method from the TorRequest object to fetch the html content. get_articles()method parses the html and returns the links in the first sentence of the article. We then put those back in the queue and also populate the result_dict. In this case, we store it as the child-parent relationship, you can store however you want.</p><p>At the end of this if we find the queue to be empty that means all the links have been worked with and no more work, so we break. Outside the loop, we close the tor connection and wait for it to get terminated. Then we delete the temporary folders.</p><p>The full code for this example can be found <a href="https://gist.github.com/zargit/ab523db6e5eb441d44eb5b0156235f63">in this gist</a>. If you want to see a more structured implementation then check out my <a href="https://github.com/zargit/rabbit-hole">rabbit-hole</a> repository, it is an extension of this work, where I generate a background trace of a definition from any given article of Wikipedia.</p><p><em>The author assumes no responsibility or liability, in any way whatsoever,</em> <em>for your use of the information contained in or linked from this article.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=645c252bd433" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>