<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Jason Hong</title>
    <description>The latest articles on DEV Community by Jason Hong (@jhong00).</description>
    <link>https://dev.to/jhong00</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F429586%2F35be6730-2c40-40ea-bfab-2f6479f2e114.png</url>
      <title>DEV Community: Jason Hong</title>
      <link>https://dev.to/jhong00</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jhong00"/>
    <language>en</language>
    <item>
      <title>Web Scraping Current Stock Prices</title>
      <dc:creator>Jason Hong</dc:creator>
      <pubDate>Sat, 11 Jul 2020 22:55:46 +0000</pubDate>
      <link>https://dev.to/bitproject/web-scraping-current-stock-prices-doh</link>
      <guid>https://dev.to/bitproject/web-scraping-current-stock-prices-doh</guid>
      <description>&lt;h1&gt;
  
  
  Why Web Scraping?
&lt;/h1&gt;

&lt;p&gt;Everyone wants to make money in the society that we live in today. More and more college students want to become engineers, computer scientists, and doctors as it secures a stable income despite its difficulties. However, what if I told you that you do not have to do this to become successful? As an investor myself, I decided to learn about stocks at a very young age. When I first started, I would try to look for the perfect brokerage. One of the important observations that I noticed was that many brokerages were late on updating the price of stocks, which would eventually lead you to losing money. Obviously, I don't want that to happen to you. So look no further! I’ve created a real time stock price scraper using Python that allows you to keep up with the current price of the stock of your interest.&lt;/p&gt;

&lt;h2&gt;
  
  
  Installation of Important Modules
&lt;/h2&gt;

&lt;p&gt;Before we begin, there are a few things we need to install. Navigate to your command line and type the following individually.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;pip install requests
pip install beautifulsoup
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;With that installed on your computer, we can then import the modules necessary for this program to work.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;bs4&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;requests&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;bs4&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;BeautifulSoup&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Keeping it concise, the bs4 module is a powerful library that allows us to access web pages, APIs, post to forms, and much more operations. The requests module allows you to send HTTP requests. Lastly, the Beautiful Soup (BS4) module sounds like a silly module but in reality it is a parsing library useful for extracting data from HTML / XML documents.&lt;/p&gt;

&lt;h2&gt;
  
  
  Implementation of Program
&lt;/h2&gt;

&lt;p&gt;For the website that I decided to web scrape, I chose a simple one &lt;em&gt;&lt;a href="https://finance.yahoo.com/quote/FB?p=FB"&gt;https://finance.yahoo.com/quote/FB?p=FB&lt;/a&gt;&lt;/em&gt;. With this url, I was able to use the requests module to access all of the response data.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'https://finance.yahoo.com/quote/FB?p=FB'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If you navigate to the link, you can see that we are interested in the Facebook stock.&lt;/p&gt;

&lt;p&gt;Currently, the price of one stock of Facebook is 234.91. This may or may not be different for you so it is perfectly fine if it is!&lt;/p&gt;

&lt;p&gt;Now we have to utilize the BS4 as well as the BeautifulSoup module, which allows us to extract data from HTML.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;soup&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;bs4&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;BeautifulSoup&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;features&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"html.parser"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We're not done yet! &lt;/p&gt;

&lt;p&gt;As you may know, a website consists of a plethora of information. From a programmers' perspective, if you tried to understand each and every function contained within the HTML/CSS tag, it would be virtually impossible. However, we can grab just the information that we need, which is the stock price (234.91 when blog was created).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;price&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;soup&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;find_all&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"div"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;'class'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'My(6px) Pos(r) smartphone_Mt(6px)'&lt;/span&gt;&lt;span class="p"&gt;})[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'span'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now we have all the information that we need to complete this function, so we can just return the price.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;To sum up, the purpose of this program is to constantly be able to know the current price of the stock that we are interested in, so we don't want sell our stock at a lower price than we intend. Therefore, we can just put the implementation above into a while loop.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The current price: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parsePrice&lt;/span&gt;&lt;span class="p"&gt;()))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;That's it! We've fully implemented a real time stock price scraper using Python. You no longer have to go through the  trouble of looking up the price of a stock. You can simply run this program! Here's how our output should look like. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--eY-yoUYA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/3xBFRuj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--eY-yoUYA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/3xBFRuj.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>productivity</category>
      <category>codenewbie</category>
      <category>beginners</category>
      <category>coding</category>
    </item>
    <item>
      <title>test</title>
      <dc:creator>Jason Hong</dc:creator>
      <pubDate>Sat, 11 Jul 2020 22:50:45 +0000</pubDate>
      <link>https://dev.to/jhong00/test-5e44</link>
      <guid>https://dev.to/jhong00/test-5e44</guid>
      <description>&lt;h1&gt;
  
  
  Why Web Scraping?
&lt;/h1&gt;

&lt;p&gt;Everyone wants to make money in the society that we live in today. More and more college students want to become engineers, computer scientists, and doctors as it secures a stable income despite its difficulties. However, what if I told you that you do not have to do this to become successful? As an investor myself, I decided to learn about stocks at a very young age. When I first started, I would try to look for the perfect brokerage. One of the important observations that I noticed was that many brokerages were late on updating the price of stocks, which would eventually lead you to losing money. Obviously, I don't want that to happen to you. So look no further! I’ve created a real time stock price scraper using Python that allows you to keep up with the current price of the stock of your interest.&lt;/p&gt;

&lt;h2&gt;
  
  
  Installation of Important Modules
&lt;/h2&gt;

&lt;p&gt;Before we begin, there are a few things we need to install. Navigate to your command line and type the following individually.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;pip install requests
pip install beautifulsoup
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;With that installed on your computer, we can then import the modules necessary for this program to work.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;bs4&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;requests&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;bs4&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;BeautifulSoup&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Keeping it concise, the bs4 module is a powerful library that allows us to access web pages, APIs, post to forms, and much more operations. The requests module allows you to send HTTP requests. Lastly, the Beautiful Soup (BS4) module sounds like a silly module but in reality it is a parsing library useful for extracting data from HTML / XML documents.&lt;/p&gt;

&lt;h2&gt;
  
  
  Implementation of Program
&lt;/h2&gt;

&lt;p&gt;For the website that I decided to web scrape, I chose a simple one &lt;em&gt;&lt;a href="https://finance.yahoo.com/quote/FB?p=FB"&gt;https://finance.yahoo.com/quote/FB?p=FB&lt;/a&gt;&lt;/em&gt;. With this url, I was able to use the requests module to access all of the response data.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'https://finance.yahoo.com/quote/FB?p=FB'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If you navigate to the link, you can see that we are interested in the Facebook stock.&lt;/p&gt;

&lt;p&gt;Currently, the price of one stock of Facebook is 234.91. This may or may not be different for you so it is perfectly fine if it is!&lt;/p&gt;

&lt;p&gt;Now we have to utilize the BS4 as well as the BeautifulSoup module, which allows us to extract data from HTML.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;soup&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;bs4&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;BeautifulSoup&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;features&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"html.parser"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We're not done yet! &lt;/p&gt;

&lt;p&gt;As you may know, a website consists of a plethora of information. From a programmers' perspective, if you tried to understand each and every function contained within the HTML/CSS tag, it would be virtually impossible. However, we can grab just the information that we need, which is the stock price (234.91 when blog was created).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;price&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;soup&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;find_all&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"div"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;'class'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'My(6px) Pos(r) smartphone_Mt(6px)'&lt;/span&gt;&lt;span class="p"&gt;})[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'span'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now we have all the information that we need to complete this function, so we can just return the price.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;To sum up, the purpose of this program is to constantly be able to know the current price of the stock that we are interested in, so we don't want sell our stock at a lower price than we intend. Therefore, we can just put the implementation above into a while loop.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The current price: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parsePrice&lt;/span&gt;&lt;span class="p"&gt;()))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;That's it! We've fully implemented a real time stock price scraper using Python. You no longer have to go through the  trouble of looking up the price of a stock. You can simply run this program! Here's how our output should look like. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--eY-yoUYA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/3xBFRuj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--eY-yoUYA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/3xBFRuj.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
