<?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 Alex Jia on Medium]]></title>
        <description><![CDATA[Stories by Alex Jia on Medium]]></description>
        <link>https://medium.com/@yaojiejia0715?source=rss-fc78bb7e4e83------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*EXfZtoSo_QUBrxgT</url>
            <title>Stories by Alex Jia on Medium</title>
            <link>https://medium.com/@yaojiejia0715?source=rss-fc78bb7e4e83------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 23:10:42 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@yaojiejia0715/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[How to use BeautifulSoup in NodeJS]]></title>
            <link>https://medium.com/@yaojiejia0715/how-to-use-beautifulsoup-in-nodejs-5aefbdc76d0e?source=rss-fc78bb7e4e83------2</link>
            <guid isPermaLink="false">https://medium.com/p/5aefbdc76d0e</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[beautifulsoup]]></category>
            <dc:creator><![CDATA[Alex Jia]]></dc:creator>
            <pubDate>Fri, 14 Jun 2024 06:01:53 GMT</pubDate>
            <atom:updated>2024-06-15T01:58:22.530Z</atom:updated>
            <content:encoded><![CDATA[<h3>Web scraping</h3><p>is sometimes illegal but a powerful technique for extracting data from websites. One of the most popular libraries for web scraping in Python is BeautifulSoup. However, if you are working in a Node.js environment and still wish to use BeautifulSoup, you might wonder how to achieve similar functionality. Fortunately, Node.js has a powerful library that can be used to connect NodeJS and Python code. In this article, we will explore how to use a combination of libraries to mimic BeautifulSoup’s functionality in Node.js.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*5l2GsmMTdvrjCGrh" /><figcaption>Photo by <a href="https://unsplash.com/@grissphoto?utm_source=medium&amp;utm_medium=referral">Max Griss</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>In this example, I will be writing a program that scrapes top investors’ stock portfolios from a desired investor, and the user will provide the prom name.</p><p>First of all, let’s import the necessary libraries</p><pre>from bs4 import BeautifulSoup<br>import requests<br>import json<br>import sys</pre><ol><li><strong>BeautifulSoup: </strong>responsible for parsing HTML elements and extracting information from these elements</li><li><strong>Requests: </strong>to fetch web pages by making HTTP GET requests to specified URLs and pass them to bs4</li><li><strong>Json: </strong>Serialize Python objects into JSON formatted string</li><li><strong>Sys: </strong>In this case, we will use it to read the command line arguments, when we are sending investor’s name we are using <strong>sys</strong> to read it in Python from NodeJS</li></ol><p>This is the full scraping script but since this article is talking about connecting NodeJS to Python BeautifulSoup, I will only focus on the connector.</p><pre>def fetch_html(url):<br>    try:<br>        headers = {<br>            &#39;User-Agent&#39;: &#39;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36&#39;,<br>            &#39;Accept-Language&#39;: &#39;en-US,en;q=0.9&#39;,<br>            &#39;Accept-Encoding&#39;: &#39;gzip, deflate, br&#39;,<br>            &#39;Accept&#39;: &#39;text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8&#39;,<br>            &#39;Connection&#39;: &#39;keep-alive&#39;<br>        }<br>        response = requests.get(url, headers=headers)<br>        return response.content<br>    except Exception as e:<br>        print(e)<br>        return [e]<br><br>def parse_links(html_content):<br>    try:<br>        soup = BeautifulSoup(html_content, &#39;html.parser&#39;)<br>        all_links = []<br>        for i in range(1, 100):<br>                a_elements = soup.select(f&#39;#port_body &gt; ul &gt; li:nth-of-type({i}) &gt; a&#39;)<br>                links = [(a.get(&#39;href&#39;), a.text.strip() if a.text else &#39;&#39;) for a in a_elements]<br>                fixed_links = [(&quot;https://www.dataroma.com/&quot; + href, text) for href, text in links]<br>                all_links.extend(fixed_links)<br>        return all_links<br>    except Exception as e:<br>        print(e)<br>        return [e]<br><br>def scrape_data_from_link(link):<br>    try:<br>        html_content = fetch_html(link)<br>        soup = BeautifulSoup(html_content, &#39;html.parser&#39;)<br>        data_rows = []<br>        for i in range(1, 100):<br>            td_elements = soup.select(f&#39;#grid &gt; tbody &gt; tr:nth-of-type({i}) &gt; td&#39;)<br>            data = [td.text.strip() if td.text else &#39;&#39; for td in td_elements]<br>            if data:<br>                data_rows.append(data[1:])<br>        return data_rows<br>    except Exception as e:<br>        print(e)<br>        return [e]<br><br>def scrape_links(investor_name):<br>    try:<br>        html_content = fetch_html(&quot;https://www.dataroma.com/m/home.php&quot;)<br>        all_links = parse_links(html_content)<br>        investor_links = [(href, name) for href, name in all_links if investor_name.lower() in name.lower()]<br>        return investor_links<br>    except Exception as e:<br>        print(e)<br>        return [e]<br><br>def scrape_data_for_investor(investor_name):<br>    try:<br>        investor_links = scrape_links(investor_name)<br>        data = []<br>        for href, name in investor_links:<br>            data.extend(scrape_data_from_link(href))<br><br>        return json.dumps(data)<br>    except Exception as e:<br>        print(e)<br>        return [e]</pre><p>Let’s transition to NodeJS for a bit, just like Python, we need to import the key library</p><pre>import express from &quot;express&quot;;<br>import {spawn} from &quot;child_process&quot;</pre><p>Let us write a simple HTTP method to get the investor’s name from the user</p><pre>app.get(&quot;/api/getByName/:name&quot;, (req, res) =&gt; {<br>    if (req.params.name === &quot;&quot;) {<br>        res.send(&quot;No name provided&quot;);<br>        return;<br>    }<br>    //use webscript to do something<br>});</pre><p>However, if we are using BeautifulSoup we have to establish a connection between these two languages, and we can use the spawn function from the “child_process” library</p><pre>app.get(&quot;/api/getByName/:name&quot;, (req, res) =&gt; {<br>    if (req.params.name === &quot;&quot;) {<br>        res.send(&quot;No name provided&quot;);<br>        return;<br>    }<br>    try {<br>        const name = req.params.name;<br>        const pythonProcess = spawn(&quot;/usr/bin/python3&quot;,[&quot;scrape.py&quot;, name]);<br>        pythonProcess.stdout.on(&#39;data&#39;, (data) =&gt; {<br>            const stocks = JSON.parse(data);<br>            res.send(stocks);<br>        });<br>    } catch (error) {<br>        console.error(error);<br>        res.send(&quot;Error occurred while scraping data: &quot; + error);<br>    }<br>});</pre><p>This line uses Node.js’s child_process module to spawn a new Python process. It runs the Python script scrape.py using Python 3 (/usr/bin/python3). The script is passed the name as an argument, which it can use for its internal processing</p><pre>const pythonProcess = spawn(&quot;/usr/bin/python3&quot;, [&quot;scrape.py&quot;, name]);</pre><p>However, the path of Python3 can be different on every machine and operating system</p><p>For Windows, you can check the path by typing</p><pre>where python3</pre><p>For Unix</p><pre>which python3</pre><p>Then, we will start handling the output from the Python process</p><pre>pythonProcess.stdout.on(&#39;data&#39;, (data) =&gt; {<br>       const stocks = JSON.parse(data);<br>       res.send(stocks);<br>   });</pre><p>pythonProcess.stdout.on(‘data’, (data) =&gt; {…}): This sets up an event listener that listens for the data event on the standard output (stdout) stream of the Python process. This event is emitted whenever the Python script writes to its stdout, typically using print(), which means we have to add print() statement to our Python code! Let’s jump back to Python and here is the full Python code:</p><pre>from bs4 import BeautifulSoup<br>import requests<br>import json<br>import sys<br><br>def fetch_html(url):<br>    try:<br>        headers = {<br>            &#39;User-Agent&#39;: &#39;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36&#39;,<br>            &#39;Accept-Language&#39;: &#39;en-US,en;q=0.9&#39;,<br>            &#39;Accept-Encoding&#39;: &#39;gzip, deflate, br&#39;,<br>            &#39;Accept&#39;: &#39;text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8&#39;,<br>            &#39;Connection&#39;: &#39;keep-alive&#39;<br>        }<br>        response = requests.get(url, headers=headers)<br>        return response.content<br>    except Exception as e:<br>        print(e)<br>        return [e]<br><br>def parse_links(html_content):<br>    try:<br>        soup = BeautifulSoup(html_content, &#39;html.parser&#39;)<br>        all_links = []<br>        for i in range(1, 100):<br>                a_elements = soup.select(f&#39;#port_body &gt; ul &gt; li:nth-of-type({i}) &gt; a&#39;)<br>                links = [(a.get(&#39;href&#39;), a.text.strip() if a.text else &#39;&#39;) for a in a_elements]<br>                fixed_links = [(&quot;https://www.dataroma.com/&quot; + href, text) for href, text in links]<br>                all_links.extend(fixed_links)<br>        return all_links<br>    except Exception as e:<br>        print(e)<br>        return [e]<br><br>def scrape_data_from_link(link):<br>    try:<br>        html_content = fetch_html(link)<br>        soup = BeautifulSoup(html_content, &#39;html.parser&#39;)<br>        data_rows = []<br>        for i in range(1, 100):<br>            td_elements = soup.select(f&#39;#grid &gt; tbody &gt; tr:nth-of-type({i}) &gt; td&#39;)<br>            data = [td.text.strip() if td.text else &#39;&#39; for td in td_elements]<br>            if data:<br>                data_rows.append(data[1:])<br>        return data_rows<br>    except Exception as e:<br>        print(e)<br>        return [e]<br><br>def scrape_links(investor_name):<br>    try:<br>        html_content = fetch_html(&quot;https://www.dataroma.com/m/home.php&quot;)<br>        all_links = parse_links(html_content)<br>        investor_links = [(href, name) for href, name in all_links if investor_name.lower() in name.lower()]<br>        return investor_links<br>    except Exception as e:<br>        print(e)<br>        return [e]<br><br>def scrape_data_for_investor(investor_name):<br>    try:<br>        investor_links = scrape_links(investor_name)<br>        data = []<br>        for href, name in investor_links:<br>            data.extend(scrape_data_from_link(href))<br><br>        return json.dumps(data)<br>    except Exception as e:<br>        print(e)<br>        return [e]<br><br><br><br><br><br>if __name__ == &quot;__main__&quot;:<br>    <br>    name = sys.argv[1]<br>    result = scrape_data_for_investor(name)<br>    print(result)<br><br></pre><pre>name = sys.argv[1]</pre><p>This line retrieves the second command-line argument passed to the script. The first argument sys.argv[0] is always the script’s filename, so sys.argv[1] is the first user-provided argument, which in this context is expected to be the name of an investor.</p><p>And the final line prints the result to the standard output (stdout). This output can be captured by other processes, such as a Node.js server, which will invoke this Python script and listen to its stdout to get the result back, as seen in the JavaScript code provided earlier.</p><p>Here is the full GitHub code and you should check it out!</p><p><a href="https://github.com/kathytran88/StockMirror">https://github.com/kathytran88/StockMirror</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5aefbdc76d0e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Deploy your Full-Stack (MERN) Side Project on AWS EC2]]></title>
            <link>https://medium.com/@yaojiejia0715/deploy-your-full-stack-mern-side-project-on-aws-ec2-628bdca65379?source=rss-fc78bb7e4e83------2</link>
            <guid isPermaLink="false">https://medium.com/p/628bdca65379</guid>
            <category><![CDATA[mern]]></category>
            <category><![CDATA[aws-ec2]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Alex Jia]]></dc:creator>
            <pubDate>Fri, 14 Jun 2024 04:59:36 GMT</pubDate>
            <atom:updated>2024-06-14T04:59:36.590Z</atom:updated>
            <content:encoded><![CDATA[<h3>Deploy your Full-Stack (MERN)Project on AWS EC2</h3><p>In today’s tech job searching, deploying a full-stack application on the cloud is an essential step to bring your project in front of the recruiters. This guide will walk you through deploying a MERN (MongoDB, Express.js, React, Node.js) stack application on AWS EC2.</p><h3>Prerequisites</h3><p>Before we go into deployment, make sure you have the following:</p><ol><li><strong>AWS Account:</strong> Sign up for an AWS account if you don’t have one.</li><li><strong>Node.js and NPM:</strong> Ensure Node.js and npm are installed on your local machine.</li><li><strong>Git:</strong> Install Git for version control.</li></ol><h3>Setting Up</h3><p>First, go to <a href="https://aws.amazon.com/ec2/">https://aws.amazon.com/ec2/</a> log in and click <strong>Launch Instance</strong> in the dashboard, then you will see the following page</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SMtgRD8dRcIGJb9FezU_UQ.png" /></figure><p>Under <strong>Quick Start, </strong>choose the operating system that you wish your project to be hosted on, it is best to choose <strong>Ubuntu</strong> for introduction but is up to your preference</p><p>Scrolling down, you will see the instance type and key pair. Instance type will determine the computing power and the memory your project will be using, again this can be adjusted based on your needs but for the sake of demonstration, we will choose the t2.micro which is included in the free tier.</p><p>We will also create a new key pair for <strong>SSH</strong> login. Be mindful that once you downloaded the key you will never be able to re-download it, so save it somewhere safe.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/810/1*tp2IApnXjTGZjT4zxlfTmA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/598/1*D-Jabip4R2612aT0YEMWZA.png" /></figure><p>Keep scrolling down and check all the options under <strong>Network Settings</strong> as they are crucial for anyone to access your future project. After that, we can hit create instance and you just started with your first AWS EC2 Instance!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/775/1*fAdPHeOvgVIX8qXzB4eaxw.png" /></figure><p>Once you created your instance, you should be taken to a dashboard like this, now on the top left just click the blue instances and go back and edit some network settings</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-KolgEK9uKaDkHk57oLD5w.png" /></figure><p>After checking your instance go to <strong>Security</strong> and click the blue security group link</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cFDdMNjq4G3HkLY0cNiQsQ.png" /></figure><p>Once we reach here click <strong>edit inbound rules</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3IbhpkikUJ6Qp41BIS4Oog.png" /></figure><p>Under <strong>Edit Inbound Rules</strong>, add two inbounds for both NodeJS and ReactJS, hopefully, you have defined the port numbers in both NodeJS and ReactJS, and in this example, NodeJS will be <strong>2000</strong> and ReactJS will be <strong>3000. </strong>Change the port range that matches your port and <strong>you have to choose 0.0.0.0/0 in order to be online, then save.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UiAQ4Ukmigpd4VXrJSn6gg.png" /></figure><p>Go back to the instance dashboard and click Connect on the top, then we will keep proceeding with everything as default values. Also, remember your public IP address for this instance!!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5YtOH9Mb8Yk60Z1mz8uQGw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/854/1*f5jg9JTw1Rsu6QvdUhtjQw.png" /></figure><p>Now you have successfully set your instance, and networks, and connected to the terminal, let’s keep moving and hosting your project!</p><p>Once we are connected, let’s write some scripts!</p><p>In case of permission issues let’s set ourselves as super users and also update the packages to date</p><pre>sudo su<br>sudo apt update</pre><p>Then we will download the necessary libraries and packages</p><pre>sudo apt install npm<br>sudo apt install nodejs<br>sudo apt install git</pre><p>After pushing your latest commit to GitHub, clone it to the EC2</p><pre>git clone &lt;your https&gt;<br>cd &lt;project directory&gt;<br>npm install</pre><p>Go to your ReactJS <strong>package.json</strong> and edit the proxy to your instance IP + your backend NodeJS port, in this case, we will set it as:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/596/1*UmPw_aNmRZPvYbEkQxWrTg.png" /></figure><p>Now the front end is connected to your backend!</p><p>This tutorial assumes you download the library <strong>concurrently</strong> and it will start ReacJS and NodeJS simultaneously in one terminal</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BJEDIc78ggrE61zPzd0jLw.png" /></figure><pre>npm start</pre><p>And now you have fully hosted your project on AWS EC2! Go to your instance IP address + ReactJS port to view your project!</p><p>Example: <a href="https://18.191.236.152:3000/">https://18.191.236.152:3000</a></p><p>If you don’t want to use <strong>concurrently, </strong>you would have to open another EC2 Instance Connect terminal, run frontend in one terminal, and run backend in another terminal.</p><p>If you are using MongoDB, go to <strong>Network Acess </strong>and add 0.0.0.0 IP address and this would allow the user to access your website anywhere</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/351/1*7jOv0ZgMAiVwYfMQv7duZg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PCONr9JgGovJSaHKnPK-ew.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=628bdca65379" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>