<?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[DREIDEV - Medium]]></title>
        <description><![CDATA[Software Engineering Business - Medium]]></description>
        <link>https://medium.com/dreidev?source=rss----d2d2541df681---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>DREIDEV - Medium</title>
            <link>https://medium.com/dreidev?source=rss----d2d2541df681---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 16:24:07 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/dreidev" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Web Scraping in Python]]></title>
            <link>https://medium.com/dreidev/web-scraping-in-python-e07fba0a1663?source=rss----d2d2541df681---4</link>
            <guid isPermaLink="false">https://medium.com/p/e07fba0a1663</guid>
            <category><![CDATA[selenium]]></category>
            <category><![CDATA[web-crawler]]></category>
            <category><![CDATA[scraping]]></category>
            <category><![CDATA[web-crawling]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Nader Alexan]]></dc:creator>
            <pubDate>Sun, 22 Apr 2018 12:54:43 GMT</pubDate>
            <atom:updated>2018-04-22T13:26:55.903Z</atom:updated>
            <content:encoded><![CDATA[<p>Motivation — At the time of writing this post, there are 1,314 posts on Upwork for web scraping projects. As I contacted a few of them and checked their requirements, it became quite obvious that their needs are not complex but rather extensive.</p><p>I started building one scraper at a time, targeting the websites the clients wanted to scrape. After three happy clients, I realized that a simple format for defining the data (data to be scrapped) and making the script generic allowed me to cut the development time from a few hours to a few minutes.</p><h3><strong>Implementation</strong></h3><p>Basic scraping follows the steps:</p><ol><li>Requesting the page — <a href="http://docs.python-requests.org/en/master/">requests library</a>, <a href="https://www.seleniumhq.org/">selenium</a></li><li>[optional] Asserting that data is loaded — custom script</li><li>Parsing the HTML — <a href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/">BeautifulSoup library</a></li><li>Retrieving the required data — <a href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/">BeautifulSoup library</a></li><li>Storing the data — custom script</li></ol><p>Each step on it’s own is quite easy to implement, it is the creation of a reusable script covering most of the cases that can be slightly tricky.</p><p><strong>Requesting the page — </strong><a href="http://docs.python-requests.org/en/master/"><strong>requests library</strong></a><strong>, </strong><a href="https://www.seleniumhq.org/"><strong>selenium</strong></a></p><p>Using the requests library is the easiest way to go. Example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6af0becd130900398c66705459d76fa7/href">https://medium.com/media/6af0becd130900398c66705459d76fa7/href</a></iframe><p>Attempt to use this with any website that loads data dynamically and you will quickly see the issue here. It requests the page as is, without allowing any JS to run on the page. Solution? Use selenium.</p><p>The code now looks this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/afed286921da3b0f0acfd87e51e631d2/href">https://medium.com/media/afed286921da3b0f0acfd87e51e631d2/href</a></iframe><p><strong>[optional] Asserting that data is loaded — custom script</strong></p><p>To assert that the data is loaded, a simple check for the existence of certain tags or text can be sufficient. For instance, consider the case of <a href="https://careers.google.com/jobs#t=sq&amp;q=j&amp;li=20&amp;l=false&amp;jlo=en-US&amp;jl=52.52000659999999%3A13.404953999999975%3ABerlin%2C+Germany%3ADE%3A%3A18.39444871486873%3ALOCALITY%3A%3A%3A%3A%3A%3A&amp;jld=20&amp;j=Python&amp;">Google Careers Search Results</a> where the results are loaded dynamically. Example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/45f0eeffbd63931faf789e5add83c1d1/href">https://medium.com/media/45f0eeffbd63931faf789e5add83c1d1/href</a></iframe><p><strong>Parsing the HTML — </strong><a href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/"><strong>BeautifulSoup library</strong></a></p><p>Parsing the html was already done in the previous step for checking that the data is loaded in the page, however, since the previous step is optional, parsing the HTML should be a step on its own.</p><p>For parsing HTML is python, there is the <a href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/">BeautifulSoup</a> library, which is both easy to use and rather powerful. It takes one line of code to parse the HTML. Example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e8faecfb16b78812a7c4d7d0a5009fe8/href">https://medium.com/media/e8faecfb16b78812a7c4d7d0a5009fe8/href</a></iframe><p><strong>Retrieving the required data — </strong><a href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/"><strong>BeautifulSoup library</strong></a></p><p>Retrieving data is straight forward using BeautifulSoup’s function find(). Example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e1d0aabc844debea6bf9a484169ceaae/href">https://medium.com/media/e1d0aabc844debea6bf9a484169ceaae/href</a></iframe><p><strong>Putting Everything Together</strong></p><p>If you’ve made it this far, you realize that implementing your own scraper in python is a pretty easy task, the challenge comes when you want to put it all together to get a generic scraper that you can use over and over again in order to finish scraping jobs in tenth of the time. That’s exactly what I went on to do, however, going through all the implementation details here would be an overkill, as well as a duplication of the scraper’s documentation (the one I built).</p><p>You can find the generic scraper with its documentation in this <a href="https://github.com/naderalexan/generic_scrapper">Github repository</a>.</p><p>On the other hand, if you choose to write your own based on the knowledge within this article, then you need to create a format for representing the tags that contain the data you want, in my case, I re-used the one that BeautifulSoup uses, with additions that you can read about in the documentation for my <a href="https://github.com/naderalexan/generic_scrapper/blob/master/scrapper.py">scraper function here</a></p><h3>Conclusion</h3><p>Web scraping can be trivial and get complex, this article attempts to explain how to build a slightly powerful, but mostly generic scraper that can be used for most of the scraping that is requested from the everyday developer, as for the more complex ones, I would recommend checking out some of the scraping libraries such as <a href="https://scrapy.org/">Scrapy</a>, or build your own.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e07fba0a1663" width="1" height="1" alt=""><hr><p><a href="https://medium.com/dreidev/web-scraping-in-python-e07fba0a1663">Web Scraping in Python</a> was originally published in <a href="https://medium.com/dreidev">DREIDEV</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Nested Pagination]]></title>
            <link>https://medium.com/dreidev/nested-pagination-md-6414a85b5501?source=rss----d2d2541df681---4</link>
            <guid isPermaLink="false">https://medium.com/p/6414a85b5501</guid>
            <category><![CDATA[django]]></category>
            <category><![CDATA[django-rest-framework]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Abdelrahmen Ayman]]></dc:creator>
            <pubDate>Sun, 22 Apr 2018 12:52:50 GMT</pubDate>
            <atom:updated>2020-10-25T00:19:09.012Z</atom:updated>
            <content:encoded><![CDATA[<p>Lately, I faced a problem while working on a RESTful API using Django and DRF, my mentor was thinking that it was solvable in one line of code, but then we googled the issue only to find that it had been addressed in several questions on Stackoverflow with <em>no accepted answer</em>, and <strong>BOOM</strong> that is where the adventure started.</p><h3>What was the problem?</h3><p>Actually the problem at first glance seems easy, <em>Paginating a set of objects inside another object</em>. Suppose we have an Author, and a Book with a <em>foreign key</em> to the Author, simple and straight forward. Now, we want to send a response containing the list of authors we are holding in our beloved database, one of the instances is representing a legendary author that has wrote a thousand books, well of course we don’t want the response to display thousand books all at once and leave no space for any other content to be displayed.</p><h3>Available solutions</h3><p>At this point we want to do something that is solvable in many ways, the most intutive solutions were:</p><ul><li>Create a separate endpoint for books and apply pagination to this endpoint.</li><li>Find a way to paginate books inside the author instance. The first solution is easy, and won’t actually take five minutes to implement, but this is not our goal here, instead, we want to make sure that our code is as clean as possible. Additionally, we wanted the books to be displayed within the author’s view not a separate one. <em>Finally, we have a very challenging mind so we decided that we will go for the second option, yes we love mystery.</em></li></ul><h3>Our Solution to the problem</h3><p>After a decent amount of time I finally found the solution to my problem and I texted my mentor after he was home to celebrate with him <strong>wohooo</strong> we have done it, I know you don’t care about those details but I am a talkative person so apologies, let’s head to the solution: <em>Firstly</em> we should think, where is the only place that we will be having control on the list of books related to that Author? You’re right, AuthorSerializer Here is our AuthorSerializer:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e6b6ae2bdc3be0d530caa136d1b6faaa/href">https://medium.com/media/e6b6ae2bdc3be0d530caa136d1b6faaa/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/daf2bb2f1cc6c1c855ecdce6cea0f773/href">https://medium.com/media/daf2bb2f1cc6c1c855ecdce6cea0f773/href</a></iframe><p><strong>Pretty simple!</strong> Cool, then we should write the function that can access the author and its books inside AuthorSerializer, just a normal function that has nothing different:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0745656dd4a07035637b6c32c9f5b076/href">https://medium.com/media/0745656dd4a07035637b6c32c9f5b076/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c08340707d762d81c6e4140f31661aa2/href">https://medium.com/media/c08340707d762d81c6e4140f31661aa2/href</a></iframe><p>Hmmmmm, let’s take this piece of code bit by bit, first the function is expecting <em>2 arguments</em> self and obj. <em>Firstly,</em> we decided that we want the number of books shown per page is 10:</p><pre>page_size = 10</pre><p>Then we used the <strong>Paginator</strong> found in <strong>django.core.paginator</strong> to paginate our desired iterable object, which is books list in our case. So we extract the books from obj and pass it to the <strong>Paginator constructor</strong> along with the page size:</p><pre>paginator = Paginator(obj.books.all(), page_size)</pre><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d2f9110b84c6caaa5c70fa76ae77212e/href">https://medium.com/media/d2f9110b84c6caaa5c70fa76ae77212e/href</a></iframe><p>Now we have in paginator many pages, each page includes 10 instances of Book, that we can access by number like this:</p><pre>books = paginator.page(1)</pre><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/76281c308492104acb921e7b54bb05de/href">https://medium.com/media/76281c308492104acb921e7b54bb05de/href</a></iframe><p>After that we serialize the books to make sure they match our BookSerializer restrictions:</p><pre>serializer = BookSerializer(books, many=True)</pre><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5d2b397c4279c89349409c0691e62475/href">https://medium.com/media/5d2b397c4279c89349409c0691e62475/href</a></iframe><p>And at the end we just return the <strong>serialized books</strong> to be used by AuthorSerializer. Seems cool but we have missed something, <em>we should actually tell our </em><em>AuthorSerializer that this function is the one used to serialize books for you</em>, so we have to add one more line to our AuthorSerializer, and we will end up having this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b6ab8a698fff0feaab224e2ef4f409a7/href">https://medium.com/media/b6ab8a698fff0feaab224e2ef4f409a7/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fd22fb8451377233492c1d78d049d110/href">https://medium.com/media/fd22fb8451377233492c1d78d049d110/href</a></iframe><p><strong>And we are done, now you can paginate objects inside another object !</strong> Now let’s have a look on some tweaks that we can add to make our code more let’s say dynamic, for now our page_size is fixed to 10 and no way to change but hard coding, so let’s change this a bit, <em>one way that I’ve found suitable to get the page size dynamically is to receive it in the request query params</em>, by doing this we can return a different response in terms of books pagination for each request:</p><pre>page_size = self.context[‘request’].query_params.get(‘size’) or 10</pre><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ea06115b433315c3a8de0b3fb0f9a281/href">https://medium.com/media/ea06115b433315c3a8de0b3fb0f9a281/href</a></iframe><p>Also or 10 is used to provide a default value in case size was not passed. We can do the same for requesting which page to access, instead of doing this:</p><pre>books = paginator.page(1)</pre><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cc4d19c7d8a08934d9ed860323d6ab0e/href">https://medium.com/media/cc4d19c7d8a08934d9ed860323d6ab0e/href</a></iframe><p>we can do this:</p><pre>page_number = self.context[&#39;request&#39;].query_params.get(&#39;page&#39;) or 1<br>books = paginator.page(page_number)</pre><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/571404dfee8facd2c8d635b651416948/href">https://medium.com/media/571404dfee8facd2c8d635b651416948/href</a></iframe><p>Now the Serializer will be like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/72c3614ca73d0403e75baa61781f7fab/href">https://medium.com/media/72c3614ca73d0403e75baa61781f7fab/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ebf4d1fe62a7150e56fabc54994e6bd2/href">https://medium.com/media/ebf4d1fe62a7150e56fabc54994e6bd2/href</a></iframe><h3>Conclusion</h3><p>Finally, this how we solved the problem we faced, if you’ve faced the same problem and if you’ve found a better solution please let us know and we can update our blog.</p><p>Also if you want another similar example you can checkout my answer to this question <a href="https://stackoverflow.com/questions/40806879/django-rest-framework-paginate-nested-object/49677960#49677960">https://stackoverflow.com/questions/40806879/django-rest-framework-paginate-nested-object/49677960#49677960</a> and maybe hit an up vote if you like it :D .</p><p>Please if you have any questions regarding this topic leave it in the comments below and I’ll be more than happy to answer.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6414a85b5501" width="1" height="1" alt=""><hr><p><a href="https://medium.com/dreidev/nested-pagination-md-6414a85b5501">Nested Pagination</a> was originally published in <a href="https://medium.com/dreidev">DREIDEV</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>