<?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 Suyog Dahal on Medium]]></title>
        <description><![CDATA[Stories by Suyog Dahal on Medium]]></description>
        <link>https://medium.com/@suyogdahal46?source=rss-ec96168b1510------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*IrboQcaHT7RWjGVZY3Ek1Q.png</url>
            <title>Stories by Suyog Dahal on Medium</title>
            <link>https://medium.com/@suyogdahal46?source=rss-ec96168b1510------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 22:19:24 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@suyogdahal46/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[Beginners Guide To Web Scraping Using Python And Beautiful-Soup]]></title>
            <link>https://medium.com/@suyogdahal46/beginners-guide-to-web-scrapping-using-python-and-beautiful-soup-7dd42eca872f?source=rss-ec96168b1510------2</link>
            <guid isPermaLink="false">https://medium.com/p/7dd42eca872f</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[requests-library]]></category>
            <category><![CDATA[pandas]]></category>
            <category><![CDATA[beautifulsoup]]></category>
            <category><![CDATA[web-scraping]]></category>
            <dc:creator><![CDATA[Suyog Dahal]]></dc:creator>
            <pubDate>Tue, 09 Apr 2019 13:25:28 GMT</pubDate>
            <atom:updated>2019-04-10T15:34:18.859Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1EgqOltlDXY2czHxBS9xTw.png" /></figure><p>In this post, you’ll learn how to use Python to write programs that will scrape a website for you in minutes what would take you hours if you were to do it manually.</p><p>So, what is web scraping? Simply put, <a href="https://en.wikipedia.org/wiki/Web_scraping"><em>web scraping</em></a> is a technique employed to extract data from a website.</p><h3><strong>What are we scraping?</strong></h3><p>We will be scraping the <a href="https://www.imdb.com/chart/top"><em>IMDb website</em></a> to extract the name, ratings and release year of the top rated 250 movies.</p><h3><strong>Setting Up!</strong></h3><p>We will first set up a <a href="https://realpython.com/python-virtual-environments-a-primer/"><em>virtual environment</em></a>, then install the requirements and then get coding!!!!</p><pre>$ pip install beautifulsoup4 requests pandas</pre><h3>Inspecting the website</h3><p>It is required to know the <a href="https://www.techopedia.com/definition/7619/html-tag"><em>HTML tags</em></a> of the elements we will be extracting. In our case the elements we will be extracting are name, ratings and release year of the movies listed on the <a href="https://www.imdb.com/chart/top"><em>site</em></a>.</p><p>Open the <a href="https://www.imdb.com/chart/top"><em>link</em></a> , hover over the movie name ,right click and click on the inspect element. You will see something similar to this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4eNXKvJ_VJ6A8G3MndnJoQ.png" /></figure><p>The movie name and the year of release is inside the &lt;td&gt; tag with class ‘titleColumn’. We can also notice that the movie name is inside &lt;a&gt; tag whereas the year of release is inside span tag.</p><p>Likewise, the rating is inside the &lt;strong&gt; tag inside of &lt;td&gt; tag with class ‘ratingColumn imdbRating’.</p><h3>Fetching The HTML</h3><p>We start by first importing all the requirements installed before in the setup section.</p><pre>import bs4<br>import requests<br>import pandas as pd</pre><p>Then fetch the HTML of the page by sending an <a href="https://www.w3schools.com/tags/ref_httpmethods.asp"><em>HTTP get request</em></a> to the desired URL and save the response to a variable</p><pre>req = requests.get(&#39;<a href="https://www.imdb.com/chart/top&#39;">https://www.imdb.com/chart/top&#39;</a>)</pre><p>You can now view the HTML of the fetched page by using req.text. But in order to extract data from the webpage, we use <a href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/"><em>BeautifulSoup</em></a> which is a very powerful Python library that helps you to extract data from the page. So, we parse the fetched page into a <a href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/#beautifulsoup"><em>soup object</em></a>.</p><pre>soup = bs4.BeautifulSoup(req.text ,&#39;html.parser&#39;)</pre><p>The soup object now contains the parsed HTML.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JaTxGx9ctNdXV0bNNQzo7Q.png" /></figure><h3>Searching the Parse Tree</h3><p>The soup object consists of navigable tree of Tag objects known as parse trees. The simplest way to navigate the parse tree is to say the name of the tag you want. For example, soup.a will return you all the objects within all the &lt;a&gt; tags.</p><p>We search the soup for the &lt;td&gt; tag with class ‘titleColumn’ as it contains the name and year of make. For this:</p><pre>name_year_container = soup.find_all(&quot;td&quot;, class_=&quot;titleColumn&quot;)</pre><p>The find_all method returns a list of tag objects. A tag object may contain strings or other tags. Our name_year_container contains two children tags &lt;a&gt; and &lt;span&gt;.</p><p>Now we loop through the list to extract movie name and make year.</p><pre>names = []<br>for i in name_year_container:<br>    names.append(i.a.text)</pre><p>The i.a.text returns the text within the &lt;a&gt; tags of the tag element. The above code can also be done in much pythonic way using <a href="https://www.pythonforbeginners.com/basics/list-comprehensions-in-python"><em>list comprehension</em></a> as:</p><pre>names = [i.a.text for i in name_year_container]<br>years = [i.span.text for i in name_year_container]</pre><p>Likewise, the ratings are extracted by:</p><pre>rating_container = soup.find_all(&#39;td&#39;, class_=&#39;ratingColumn imdbRating&#39;)</pre><pre>ratings = [i.strong.text for i in rating_container] #td&gt;strong</pre><p>Thus, now we have successfully extracted data from the webpage and stored them temporarily as lists. In order to store the data permanently, we export the data to a CSV file.</p><p>A powerful tabular data manipulation library Pandas can be used for the converting lists to CSV file.</p><pre>df = pd.DataFrame({<br>    &#39;Names&#39;: names,<br>    &#39;Years&#39;: years,<br>    &#39;Ratings&#39;: ratings,<br>})</pre><p>Your data is converted into a pandas <a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.html"><em>DataFrame</em></a>. Don’t worry if you don’t know what it is. In short, it is a tabular representation of the data. You can view the first five rows of the <a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.html"><em>DataFrame</em></a> using df.head().</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/302/1*FiCSWZ70iF4M9JakUHZGOg.png" /></figure><p>Finally export the data into a CSV by:</p><pre>df.to_csv(&#39;IMDb.csv&#39;, index=False)</pre><p>Hope the article was helpful for you to get started with web scraping.</p><p>Thankyou!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7dd42eca872f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>