<?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 Marco Cuciniello on Medium]]></title>
        <description><![CDATA[Stories by Marco Cuciniello on Medium]]></description>
        <link>https://medium.com/@marcocuciniello?source=rss-6a7911305974------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*CGLQr5LLWHmfyrYy3dXIGQ.jpeg</url>
            <title>Stories by Marco Cuciniello on Medium</title>
            <link>https://medium.com/@marcocuciniello?source=rss-6a7911305974------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 06:13:34 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@marcocuciniello/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[Writing scrapers the good way]]></title>
            <link>https://medium.com/@marcocuciniello/writing-scrapers-the-good-way-b7d308a78a1f?source=rss-6a7911305974------2</link>
            <guid isPermaLink="false">https://medium.com/p/b7d308a78a1f</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[api]]></category>
            <category><![CDATA[scraping]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Marco Cuciniello]]></dc:creator>
            <pubDate>Mon, 04 Nov 2019 16:03:10 GMT</pubDate>
            <atom:updated>2019-11-13T14:43:39.195Z</atom:updated>
            <content:encoded><![CDATA[<p>If you start with <strong>from</strong> bs4 <strong>import</strong> BeautifulSoup 95 % of the times you are doing it wrong.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/970/1*gSGUH-HUPkuLZUzSLuWzDQ.png" /><figcaption>(burp community edition is enough to follow this tutorial)</figcaption></figure><p><em>(not) important disclaimer: always check the policy of the website you are trying to scrape, this is only for educational purposes</em></p><h3>Small Intro</h3><blockquote>this can be skipped like I always do when I read a book</blockquote><p>Usually when you find an article about scraping in python 3 (stop using python 2.7.x please) you will find something that can be summarized like this:</p><ul><li>How to install <strong>python</strong> (if you are reading a tutorial on how to write a scraper in python and you don’t have python installed maybe it’s better to start with something else)</li><li>How to install <strong>BeautifulSoup4</strong></li><li>How to scrape a website with a super complex HTML like <strong>&lt;html&gt;&lt;div class=’please_scrape_me’&gt;</strong>hello world!<strong>&lt;/div&gt;&lt;/html&gt;</strong></li><li>End</li></ul><p>In this article I want to analyze a real world case where we will scrape finance news data from Yahoo implementing a private API that take as an input a ticker symbol and returns us the news, without lot of lines and complex regex to extract data from the HTML but simply using plain HTTPS requests.</p><h3>HTML Parsing Vs REST APIs</h3><p>I don’t think that there are pros parsing html instead of using the website rest APIs. Some of the pros of going REST are:</p><ul><li>Data is structured and already formatted in json or xml</li><li>HTML in websites can change frequently, the APIs don’t</li><li>If your rest api was reversed from a mobile app usually is not going to change fast, because if they break your scraper they break their old builds</li></ul><h3>Burp suite</h3><blockquote>currently I use it even as a browser</blockquote><p>For analyzing and playing with HTTP / HTTPS / websocket requests burp is one of the most useful and complete tools you can use and the free community edition have almost everything you need. I will avoid using professional feature only.</p><p>Yes, you can analyze the traffic using other tools or the browser dev console, but if you get stuck with the website and you need to analyze the traffic of an iOS / Android app, burp is one of the best choices.</p><p>The setup instructions refer to macOS but you can easily find the equivalent on windows / linux. If you already have a working burp proxy setup just skip to the Information gathering section.</p><ol><li>Download and install burp suite, then click on the <strong>proxy tab</strong>, <strong>options</strong>, <strong>add</strong> and insert a port in the <strong>bind to port</strong> field, select <strong>all interfaces</strong> and confirm.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*o_HGUjT74v2mbxjZ3iZ0tw.png" /></figure><p>2. Now we must connect to our local burp proxy, instead of changing your proxy settings every time, let’s create a new macOS network location. Open <strong>System Preferences</strong>, <strong>Network</strong>, Click on the <strong>Location</strong> dropdown menu and <strong>Edit Locations</strong>, click on the <strong>+</strong> icon and add a new location with the name Burp.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/673/1*2XjlFbcS9pXxJE_Ce4_sBA.png" /></figure><p>3. Once you selected the new location go to <strong>Advanced</strong>, <strong>Proxies</strong>, select both <strong>Web Proxy (HTTP) </strong>and <strong>Secure Web Proxy (HTTPS)</strong> and enter <strong>localhost:8082</strong> in the address field.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/664/1*w3MR02nztHxtL1xTm47INQ.png" /></figure><p>Every time you want to switch to the local burp proxy you can just select it from the <strong>Locations</strong> dropdown or by clicking on it from the Apple logo menu in the left corner of the screen.</p><p>4. We need to configure the SSL certificate, after selecting the Burp location just visit <a href="http://burp/">http://burp</a> in your browser and you will see a screen similar to this, click on <strong>CA Certificate</strong> and install it</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/790/1*tW828cTu4Bvc6aUCc1C3Ow.png" /></figure><p>If everything works correctly every request will be forwarded to burp and you will be able to <strong>forward</strong>, <strong>drop</strong>, <strong>modify</strong> each request. In Burp go to the <strong>Intercept</strong> tab and check that <strong>Intercept</strong> is on, then open <a href="https://google.com/">https://google.com</a> from your browser and you will see the request with all the parameters in burp</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/924/1*fjGBadqR2KN6DOgv2Oc_3Q.png" /></figure><h3>Information gathering</h3><p>First thing to do is to browse the website and check which requests are made and in which one they return the data we want. Let’s go to the finance section and see which requests are made if we search “Apple” in the searchbar and by opening the relative page, intercepting and forwarding the request one by one (or you can just disable intercept and go into the <strong>HTTP history</strong> and search there) you will notice that the news will not load until you will forward a request to <em>/quote/AAPL?p=AAPL&amp;.tsrc=fin-src </em>so probably this is the request that contains the data we want, let’s head to the <strong>HTTP history</strong> tab and check the response.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jmVG66vc0qL1V9RUmE6w_A.png" /></figure><p>if we try to search a news we can see on the website we will confirm that the news are in the response to this GET /quote/</p><blockquote>oh so bad they returns HTML!!1 we must fallback to HTML parsing!!</blockquote><p><strong>No.</strong></p><p>Currently almost every website have REST APIs that provides data to the frontend, but sometimes like in this case, I selected Yahoo because perfectly fit the tutorial, they return only HTML to the frontend. If it’s rare to find a website without REST APIs, it’s almost impossible to find an app without a REST APIs. That is why i wrote in the title that the 95% of the times HTML parsing is not needed, but obviously there are few cases where the website returns only HTML and there isn’t a mobile app available and in those cases HTML parsing is needed.</p><h3>App Store time</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*l349zRCpxS4McAxmxiosrA.jpeg" /></figure><p>First download the app from the app store or the play store, then configure your device to work with burp proxy.</p><p>iOS:</p><p><a href="https://support.portswigger.net/customer/portal/articles/1841108-configuring-an-ios-device-to-work-with-burp">Configuring an iOS Device to Work With B...</a></p><p>Android:</p><p><a href="https://support.portswigger.net/customer/portal/articles/1841101-configuring-an-android-device-to-work-with-burp">Configuring an Android Device to Work Wi...</a></p><p>Remember to go to <a href="http://burp/">http://burp</a> after the setup and install the CA Certificate or you will get SSL errors on every HTTPS request.</p><p>Now let’s search for the same data on the app and see which requests are made.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*b7wLoxUwVjO6GUDlj6Setw.png" /><figcaption>bingo!</figcaption></figure><p>News are loaded in the app after this GET <em>/dp/v2/newsfeed</em> is made, by inspecting the content in the response we can see that is a json with all the news available on the website and in the app!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WqBbM4rvzmuDAvL2qjhcGw.png" /></figure><p>In the request tab we need to take note of the url and the user agent that as you can see is specific for the Yahoo app</p><h3>Python implementation</h3><p>Before starting coding your script, you can play a bit with the parameters, for example <em>count</em> can be changed to an higher number than the default 10 and the request will work without problems, more results with a single call, less requests, less probability of being blocked.</p><p>We can also see that <em>mfin</em> is optional and if we remove it nothing changes. Now instead of using a lot of <em>find_all</em>, <em>find</em>, <em>xpath</em>, <em>css selectors,</em> you can simply make a GET request using the requests module like this</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a57f0d6691e7511f4665a2217b5edc6a/href">https://medium.com/media/a57f0d6691e7511f4665a2217b5edc6a/href</a></iframe><p>and that’s it! We have all the news we can see on the website / app already structured in a json that we can easily parse and save to a database or file.</p><p>It’s also easy to add a paging system to get all the news after a certain date, we just need to send the last <em>article_uuid </em>returned in each request in the next one.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1b836b79b161dec6b7a85fd53a9ea0e9/href">https://medium.com/media/1b836b79b161dec6b7a85fd53a9ea0e9/href</a></iframe><p>The script is super simple, from there you can structure it with classes, add a proxy rotation system, save the news to a db, add checks on the data returned and all the things that can fit your use case.</p><h3>Conclusions</h3><p>We’ve seen the basics on how to find which APIs returns the data we want and how to implement it in Python. By using APIs instead of HTML parsing you will have a lot more stable scrapers that don’t require you to update <em>xpaths</em> / <em>class selectors</em> every week. In the next article we will see how to scale scrapers like this one with the help of an asynchronous task queue like celery and some workaround in other difficult scenarios.</p><p>If you have any questions just write it in the comment or contact me on <a href="https://www.instagram.com/marco_cuciniello/">Instagram</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b7d308a78a1f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>