<?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 Aruna Rathnayake on Medium]]></title>
        <description><![CDATA[Stories by Aruna Rathnayake on Medium]]></description>
        <link>https://medium.com/@aruna1rathnayake?source=rss-0b67f8abaee8------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*bRc7Uy8cIQFt9eNS</url>
            <title>Stories by Aruna Rathnayake on Medium</title>
            <link>https://medium.com/@aruna1rathnayake?source=rss-0b67f8abaee8------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 09:25:12 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@aruna1rathnayake/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[Web Scraping Ebay with python part 1: A simple single product scrape]]></title>
            <link>https://medium.com/@aruna1rathnayake/web-scraping-ebay-with-python-part-1-a-simple-single-product-scrape-d3de88d45273?source=rss-0b67f8abaee8------2</link>
            <guid isPermaLink="false">https://medium.com/p/d3de88d45273</guid>
            <category><![CDATA[beautifulsoup]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[web-scraping]]></category>
            <category><![CDATA[data-engineering]]></category>
            <category><![CDATA[data-science]]></category>
            <dc:creator><![CDATA[Aruna Rathnayake]]></dc:creator>
            <pubDate>Wed, 18 Dec 2024 02:32:26 GMT</pubDate>
            <atom:updated>2024-12-18T02:32:26.203Z</atom:updated>
            <content:encoded><![CDATA[<p>Data Extraction, Data Transformation, and Data Loading, known as ETL, are the three basic responsibilities of a Data Engineer. Speaking about Extraction, there are many ways to extract data for a project. Web Scraping is one of them. In this project, let’s scrape watch data from the eBay website</p><p>In part 1 of this project, let’s try to scrape a single product to understand how the website’s HTML works and to get a better understanding of the scope of our web scraping project.</p><h4>Prerequisites</h4><ul><li>Basic knowledge of python.</li><li>Basic knowledge of HTML and how websites HTML tags work</li><li>Jupyter Notebook</li></ul><h4>The Webpage:</h4><p>In this project, we are scraping watch data from the <a href="https://www.ebay.com/b/Watches/260325/bn_7117208191"><strong><em>Watches | eBay</em></strong></a><strong> </strong>webpage. I just accessed <a href="http://www.ebay.com/"><em>www.ebay.com</em></a> and navigated to the watch subcategory under the fashion category. Here is a snapshot of the page.</p><figure><img alt="A snapshot of the webpage that is used in this project for web scraping" src="https://cdn-images-1.medium.com/max/1024/1*-DERnQWnO98sUrHPrSw_4A.png" /><figcaption>A snapshot of the webpage that is used in this project for web scraping</figcaption></figure><p>In this part 1, we will web scrape the first product from the above listed products.</p><p>Click on the first product and access the product’s web page. In my case it is <a href="https://www.ebay.com/itm/405017586079?epid=2254688294&amp;itmmeta=01JF87H99PD00RPRDFE9XSPY83&amp;hash=item5e4cede99f:g:VsMAAOSwomFl1Pfv&amp;itmprp=enc%3AAQAJAAAA4Mxmj%2BiGvOveHXEBClPb29hr9xQfZg%2B0w1iZTqjQgElox8DMV%2FmXA8VoeI7Q%2BWia%2BbVsi%2B%2FtkCLaiP9UWl9WnoCWIf43SK1qdOf86YxkMXfMzeH1CDlDXgShB2c1yENg7yGX%2FQ%2Bu3xJG7f5QIHsdwnNGmjl6r6LBhx7EUMs6X3%2FBsd1dZObCCP2xPLpVt9%2BxGVs%2Fms9LUc3CvOnyHkQGU1JMaSO4jxJovXzEfe9K%2Bn1srAr2bloWcS2yTRJZYag4mk782h2hMtsUF5Oh79oH3Qj0tD0%2BrxglmshyI2vuG6wu%7Ctkp%3ABFBM-JTFh_pk"><em>Gucci Women’s Sync L White Rubber Strap Quartz Watch</em></a><em> . </em>Here is how product’s page should look like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fcvqtOK9t19j0qJCx5_Tpg.png" /><figcaption>A snapshot of the selected product’s webpage (Gucci Women’s Sync L White Rubber Strap Quartz Watch)</figcaption></figure><h4>1. Setup the connection</h4><p>Let us open a Jupyter notebook and install three python libraries those are be used in this project:</p><ol><li><strong>Beautiful soup</strong> — To phrase the HTML code.</li><li><strong>Requests</strong> — To send requests from local PC to the website.</li><li><strong>Pandas</strong> — To convert data into dataframes.</li></ol><p>Let’s install above libraries in Jupyter notebook by running below commands:</p><pre>!pip install bs4<br>!pip install requests<br>!pip install pandas</pre><p>Import above installed libraries to the notebook environment so that we can use them in our project:</p><pre>from bs4 import BeautifulSoup<br>import requests<br>import pandas as pd</pre><p>Next, let’s send HTTP requests to the URL of the webpage. To do this, we need to know the URL and the headers for our request. We already know the URL. Headers contain information about the browser, computer, and the language we use to make this request. To get header data, you need to find your user agent first. To get your user agent data, search ‘what is my user agent’ on Google.</p><figure><img alt="Results for the google search prompt “What is my user agent”" src="https://cdn-images-1.medium.com/max/1024/1*xha9KF80VFtzfTGVHnheZQ.png" /><figcaption>Results for the google search prompt “What is my user agent”</figcaption></figure><p>In first search result Google shows you your user agent. This has information about your browser and the computer. Copy this result.</p><p>A HEADER variable contains two parts. First part is the User-Agent. the second part is the Accept-Language. Here is a template for HEADERS variable:</p><pre>HEADERS = ({ &#39;User-Agent&#39; : &#39;{AGENT INFORMATION}&#39;, &#39;Accept-Language&#39;: &#39;{LANGUAGE INFORMATION}&#39; })</pre><p>Switch back to the Jupyter notebook and define the HEADER variable. Replace {AGENT INFORMATION} with the agent information you copied from Google.</p><p>For Accept-Language, replace {LANGUAGE INFORMATION} with your desired language. In my case, I am replacing it with ‘en-US, en;q=0.5&#39;</p><ul><li><strong>en-US</strong>: American English (en-US) as the primary language.</li><li><strong>en;q=0.5</strong>: General English (en), but with a lower priority (quality value q=0.5).</li></ul><p>By the end HEADERS variable should be defined in the notebook like this. (I have hidden middle section of my user agent with ‘…’ for privacy.)</p><pre># Headers for request<br>HEADERS = ({ &#39;User-Agent&#39; : &#39;Mozilla/5.0 ... Edg/131.0.0.0&#39;, &#39;Accept-Language&#39;: &#39;en-US, en;q=0.5&#39; })</pre><p>Next, let’s define the URL to our web page. Store the URL in a variable called URL:</p><pre>URL = &quot;https://www.ebay.com/itm/405017586079?epid=2254688294&amp;itmmeta=01JF87H99PD00RPRDFE9XSPY83&amp;hash=item5e4cede99f:g:VsMAAOSwomFl1Pfv&amp;itmprp=enc%3AAQAJAAAA4Mxmj%2BiGvOveHXEBClPb29hr9xQfZg%2B0w1iZTqjQgElox8DMV%2FmXA8VoeI7Q%2BWia%2BbVsi%2B%2FtkCLaiP9UWl9WnoCWIf43SK1qdOf86YxkMXfMzeH1CDlDXgShB2c1yENg7yGX%2FQ%2Bu3xJG7f5QIHsdwnNGmjl6r6LBhx7EUMs6X3%2FBsd1dZObCCP2xPLpVt9%2BxGVs%2Fms9LUc3CvOnyHkQGU1JMaSO4jxJovXzEfe9K%2Bn1srAr2bloWcS2yTRJZYag4mk782h2hMtsUF5Oh79oH3Qj0tD0%2BrxglmshyI2vuG6wu%7Ctkp%3ABFBM-JTFh_pk&quot;</pre><p>Now let’s send an HTTP request to the webpage using our HEADER and the URL to see if it is successful:</p><pre># HTTP request<br>webpage = requests.get(URL, headers=HEADERS)<br>webpage</pre><p>By running the above command, we should get the output &lt;Response [200]&gt;, which means the HTTP request was successful.</p><h4>2. Identify HTML tags to extract necessary data</h4><p>Now that we have established the HTTP connection to the desired webpage, let’s identify the data we need to extract. In this web scraping project part 1, we are scraping:</p><ul><li>Product name</li><li>Price</li></ul><p>from the product.</p><p>To scrape this data, we need to understand the location within the HTML code of the website where this information is found.</p><p>First, let’s retrieve the HTML code of this website to our notebook so that we can scrape data from it:</p><pre>webpage.content</pre><p>The above code retrieves the HTML code from the website. However, the HTML code is in bytes format and not in HTML format.</p><figure><img alt="Retrieved unproperly formatted HTML code" src="https://cdn-images-1.medium.com/max/1024/1*TwN7xJ5fdF2AfSU2eaoeZg.png" /><figcaption>Retrieved unproperly formatted HTML code</figcaption></figure><p>To properly format the retrieved HTML code, we can use the BeautifulSoup Python library:</p><pre>soup = BeautifulSoup(webpage.content, &#39;html.parser&#39;)<br>soup</pre><p>By running the above code, we can get the HTML code properly formatted.</p><p>Now, let us find in within which HTML tags, Product Name and Price data are stored in.</p><p>Go to the webpage of the product we are scraping data from.</p><h4>3. Scrape product name</h4><ul><li><strong>1. Select the product name:</strong></li></ul><p>Right-click on the product name and select “Inspect” from the menu. This will open up the HTML code, highlighting the part we selected.</p><figure><img alt="Using inspect element to open up the HTML code of the product name" src="https://cdn-images-1.medium.com/max/1024/1*H86QEj4pdPMq9hUfCEivgg.png" /><figcaption>Using inspect element to open up the HTML code of the product name</figcaption></figure><ul><li><strong>2. Inspect Element:</strong></li></ul><p>As we can see below, the product name is found in a span class named “ux-textspans ux-textspans — BOLD”. However, the product HTML class name is found in an h1 class named “x-item-title__mainTitle” surrounding the span with “ux-textspans ux-textspans — BOLD”.</p><figure><img alt="Inspect element result HTML code for the product name" src="https://cdn-images-1.medium.com/max/571/1*o8QHIinQEg0JPNQrY24jXQ.png" /><figcaption>Inspect element result HTML code for the product name</figcaption></figure><ul><li><strong>3. Scrape Contents:</strong></li></ul><p>First, let’s scrape the contents in the h1 class tag:</p><pre>soup.find(&quot;h1&quot;, attrs={&quot;class&quot;:&#39;x-item-title__mainTitle&#39;})</pre><p>You might see a result like this:</p><pre>&lt;h1 class=&quot;x-item-title__mainTitle&quot;&gt;&lt;!--F#0--&gt;&lt;!--F#f_1--&gt;&lt;!--F#11[0]--&gt;&lt;span class=&quot;ux-textspans ux-textspans--BOLD&quot;&gt;Gucci Women&#39;s Sync L White Rubber Strap Quartz Watch - YA137302 ($670 MSRP)&lt;/span&gt;&lt;!--F/--&gt;&lt;!--F/--&gt;&lt;!--F/--&gt;&lt;/h1&gt;</pre><ul><li><strong>4. Scrape Span Contents:</strong></li></ul><p>Now, we should scrape the contents in the span class named “ux-textspans ux-textspans — BOLD” within the above h1 tag:</p><pre>soup.find(&quot;h1&quot;, attrs={&quot;class&quot;:&#39;x-item-title__mainTitle&#39;}).find(&quot;span&quot;, attrs={&quot;class&quot;: &quot;ux-textspans ux-textspans--BOLD&quot;})</pre><p>You might see a result like this:</p><pre>&lt;span class=&quot;ux-textspans ux-textspans--BOLD&quot;&gt;Gucci Women&#39;s Sync L White Rubber Strap Quartz Watch - YA137302 ($670 MSRP)&lt;/span&gt;</pre><ul><li><strong>5. Extract Text:</strong></li></ul><p>To eliminate HTML tags and keep only the text (which is the product name), add .text to the end of the code:</p><pre>soup.find(&quot;h1&quot;, attrs={&quot;class&quot;:&#39;x-item-title__mainTitle&#39;}).find(&quot;span&quot;, attrs={&quot;class&quot;: &quot;ux-textspans ux-textspans--BOLD&quot;}).text</pre><ul><li><strong>6. Result</strong></li></ul><p>By the end of this, the result we get is the product name:</p><pre>&quot;Gucci Women&#39;s Sync L White Rubber Strap Quartz Watch - YA137302 ($670 MSRP)&quot;</pre><h4>4. Scrape product price</h4><ul><li><strong>1. Select the Product Price:</strong></li></ul><p>Right-click on the product price and select “Inspect” from the menu. This will open up the HTML code, highlighting the part we selected.</p><figure><img alt="Using inspect element to open up the HTML code of the product price" src="https://cdn-images-1.medium.com/max/1024/1*lSgxwFMO7iusQOL4neb5LQ.png" /><figcaption>Using inspect element to open up the HTML code of the product price</figcaption></figure><p>As we can see below, the price is found in a span class named “ux-textspans”. However, the price HTML class name is found in a div class named “x-price-primary” surrounding the span with “ux-textspans”.</p><figure><img alt="Inspect element result HTML code for the product price" src="https://cdn-images-1.medium.com/max/701/1*SxnRDxlWgKzI3avWERWj5w.png" /><figcaption>Inspect element result HTML code for the product price</figcaption></figure><ul><li><strong>3. Scrape Contents</strong></li></ul><p>First, let’s scrape the contents in the div class tag:</p><pre>soup.find(&quot;div&quot;, attrs={&quot;class&quot;:&#39;x-price-primary&#39;})</pre><p>You might see a result like this:</p><pre>&lt;div class=&quot;x-price-primary&quot; data-testid=&quot;x-price-primary&quot;&gt;&lt;!--F#0--&gt;&lt;!--F#f_1--&gt;&lt;!--F#11[0]--&gt;&lt;span class=&quot;ux-textspans&quot;&gt;US $269.99&lt;/span&gt;&lt;!--F/--&gt;&lt;!--F/--&gt;&lt;!--F/--&gt;&lt;/div&gt;</pre><ul><li><strong>4. Scrape Span Contents:</strong></li></ul><p>Now, we should scrape the contents in the span class named “ux-textspans” within the above div tag:</p><pre>soup.find(&quot;div&quot;, attrs={&quot;class&quot;:&#39;x-price-primary&#39;}).find(&quot;span&quot;, attrs={&quot;class&quot;: &quot;ux-textspans&quot;})<br></pre><p>You might see a result like this:</p><pre>&lt;span class=&quot;ux-textspans&quot;&gt;US $269.99&lt;/span&gt;</pre><ul><li><strong>5. Extract Text</strong></li></ul><p>To eliminate HTML tags and keep only the text (which is the price), add .text to the end of the code:</p><pre>soup.find(&quot;div&quot;, attrs={&quot;class&quot;:&#39;x-price-primary&#39;}).find(&quot;span&quot;, attrs={&quot;class&quot;: &quot;ux-textspans&quot;}).text</pre><ul><li><strong>6. Result</strong></li></ul><p>By the end of this, the result we get is the price:</p><pre>&#39;US $269.99&#39;</pre><h4>5. Conclusion</h4><p>The web scraping project we covered here is a basic introduction for beginners. However, in a real-world environment, scraping data one by one like this is not sufficient. We need to scrape multiple pieces of data from various elements on a webpage. In part 2 of this project, let’s enhance our skills by scraping data from multiple products at once.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d3de88d45273" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>