<?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: Alireza Mika</title>
    <description>The latest articles on DEV Community by Alireza Mika (@alirezamika).</description>
    <link>https://dev.to/alirezamika</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%2F461055%2F4dc1e71a-8bd3-4a84-8039-0adb6c01b218.jpeg</url>
      <title>DEV Community: Alireza Mika</title>
      <link>https://dev.to/alirezamika</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/alirezamika"/>
    <language>en</language>
    <item>
      <title>AutoScraper and Flask: Create an API From Any Website in Less Than 5 Minutes</title>
      <dc:creator>Alireza Mika</dc:creator>
      <pubDate>Mon, 21 Sep 2020 09:44:01 +0000</pubDate>
      <link>https://dev.to/alirezamika/autoscraper-and-flask-create-an-api-from-any-website-in-less-than-5-minutes-400j</link>
      <guid>https://dev.to/alirezamika/autoscraper-and-flask-create-an-api-from-any-website-in-less-than-5-minutes-400j</guid>
      <description>&lt;p&gt;In this tutorial, we are going to create our own e-commerce search API with support for both eBay and Etsy without using any external APIs. With the power of &lt;a href="https://github.com/alirezamika/autoscraper/" rel="noopener noreferrer"&gt;AutoScraper&lt;/a&gt; and &lt;a href="https://flask.palletsprojects.com/" rel="noopener noreferrer"&gt;Flask&lt;/a&gt;, we are able to achieve this goal in fewer than 20 lines of Python code for each site. I recommend reading my last &lt;a href="https://medium.com/better-programming/introducing-autoscraper-a-smart-fast-and-lightweight-web-scraper-for-python-20987f52c749" rel="noopener noreferrer"&gt;article&lt;/a&gt; about AutoScraper if you haven’t done so yet.&lt;/p&gt;




&lt;h1&gt;
  
  
  Requirements
&lt;/h1&gt;

&lt;p&gt;Install the required libraries using pip:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install -U autoscraper flask
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Let’s Do It
&lt;/h1&gt;

&lt;p&gt;First, we are going to create a smart scraper to fetch data from eBay’s search results page. Let’s say we want to get the &lt;code&gt;title&lt;/code&gt;, &lt;code&gt;price&lt;/code&gt;, and &lt;code&gt;product link&lt;/code&gt; of each item. Using AutoScraper, it would be easily done by just providing some sample data:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;Note that if you want to copy and run this code, you may need to update the &lt;code&gt;wanted_list&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now let’s get the results grouped by scraping rules:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;scraper.get_result_similar(url, grouped=True)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;From the output, we’ll know which rule corresponds to which data, so we can use it accordingly. Let’s set some aliases based on the output, remove redundant rules, and save the model so we can use it later:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;Note that the rule IDs will be different for you if you run the code.&lt;/p&gt;

&lt;p&gt;OK, we’ve got eBay covered. Let’s add support for Etsy search results too. We’ll start by building its scraper. This time, we will use &lt;code&gt;wanted_dict&lt;/code&gt; instead of &lt;code&gt;wanted_list&lt;/code&gt;. It will automatically set aliases for us:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;As the links are generated with a unique ID on Etsy each time, we added one sample product ID to the &lt;code&gt;wanted_dict&lt;/code&gt; so we can create the link from it. Also, we provided two samples for title and price, as the structure of items on Etsy search result pages is different and we want the scraper to learn them all.&lt;/p&gt;

&lt;p&gt;After analyzing the output, let’s keep our desired rules, remove the rest, and save our model:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Now that we have our scrapers ready, we can create our fully functioning API for both sites in fewer than 40 lines:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Here, we are defining an API with the parameter &lt;code&gt;q&lt;/code&gt; as our search query. We will get and join eBay and Etsy search results and return them as a response. Note that we are passing &lt;code&gt;group_by_alias=True&lt;/code&gt; to the scraper to get the results grouped by our defined aliases.&lt;/p&gt;

&lt;p&gt;By running this code, the API server will be up listening on port 8080. So let’s test our API by opening &lt;code&gt;http://localhost:8080/?q=headphone&lt;/code&gt; in our browser:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F5744%2F1%2Ai13MQE455mvv7ouOZoOC6Q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F5744%2F1%2Ai13MQE455mvv7ouOZoOC6Q.png"&gt;&lt;/a&gt;&lt;br&gt;Some results from eBay
  &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F5752%2F1%2AUJt7y3MXB1xheBwDPWiN1Q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmiro.medium.com%2Fmax%2F5752%2F1%2AUJt7y3MXB1xheBwDPWiN1Q.png"&gt;&lt;/a&gt;&lt;br&gt;Some results from Etsy
  &lt;/p&gt;

&lt;p&gt;Voila! We have our e-commerce API ready. Just replace &lt;code&gt;headphone&lt;/code&gt; in the URL with your desired query to get its search results.&lt;/p&gt;




&lt;h1&gt;
  
  
  Final Notes
&lt;/h1&gt;

&lt;p&gt;The final code for this tutorial is available on &lt;a href="https://github.com/alirezamika/tutorials/tree/master/api_server" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This is a development setup suitable for developing and testing. Flask’s built-in server is not suitable for production. For production usage, please check &lt;a href="https://flask.palletsprojects.com/en/1.1.x/deploying/" rel="noopener noreferrer"&gt;Flask’s deployment options&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This tutorial is intended for personal and educational use. If you want to scrape websites, you can check their policies regarding the scraping bots.&lt;/p&gt;

&lt;p&gt;I hope this article is useful and helps to bring your ideas into code faster than ever. Happy coding!&lt;/p&gt;

</description>
      <category>python</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Smart Automatic Web Scraping in Python</title>
      <dc:creator>Alireza Mika</dc:creator>
      <pubDate>Tue, 01 Sep 2020 08:16:34 +0000</pubDate>
      <link>https://dev.to/alirezamika/smart-automatic-web-scraping-in-python-23bp</link>
      <guid>https://dev.to/alirezamika/smart-automatic-web-scraping-in-python-23bp</guid>
      <description>&lt;p&gt;In the last few years, web scraping has been one of my day to day and frequently needed tasks. I was wondering if I can make it smart and automatic to save lots of time. So I made AutoScraper!&lt;/p&gt;

&lt;p&gt;The project code is available on github:&lt;br&gt;
&lt;a href="https://github.com/alirezamika/autoscraper/"&gt;https://github.com/alirezamika/autoscraper/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This project is made for automatic web scraping to make scraping easy. &lt;br&gt;
It gets a url or the html content of a web page and a list of sample data which we want to scrape from that page. This data can be text, url or any html tag value of that page. It learns the scraping rules and returns the similar elements. Then you can use this learned object with new urls to get similar content or the exact same element of those new pages!&lt;/p&gt;
&lt;h2&gt;
  
  
  Installation
&lt;/h2&gt;

&lt;p&gt;Install latest version from git repository using pip:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;git+https://github.com/alirezamika/autoscraper.git
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  How to use
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Getting similar results
&lt;/h3&gt;

&lt;p&gt;Say we want to fetch all related post titles in a stackoverflow page:&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;from&lt;/span&gt; &lt;span class="nn"&gt;autoscraper&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;AutoScraper&lt;/span&gt;

&lt;span class="n"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;'https://stackoverflow.com/questions/2081586/web-scraping-with-python'&lt;/span&gt;

&lt;span class="c1"&gt;# We can add one or multiple candidates here.
# You can also put urls here to retrieve urls.
&lt;/span&gt;&lt;span class="n"&gt;wanted_list&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"How to call an external command?"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="n"&gt;scraper&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;AutoScraper&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;scraper&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;build&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;wanted_list&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="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here's the output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="s"&gt;'How do I merge two dictionaries in a single expression in Python (taking union of dictionaries)?'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="s"&gt;'How to call an external command?'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="s"&gt;'What are metaclasses in Python?'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="s"&gt;'Does Python have a ternary conditional operator?'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="s"&gt;'How do you remove duplicates from a list whilst preserving order?'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="s"&gt;'Convert bytes to a string'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="s"&gt;'How to get line count of a large file cheaply in Python?'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="s"&gt;"Does Python have a string 'contains' substring method?"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="s"&gt;'Why is “1000000000000000 in range(1000000000000001)” so fast in Python 3?'&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now you can use the &lt;code&gt;scraper&lt;/code&gt; object to get related topics of any stackoverflow page:&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;scraper&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get_result_similar&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'https://stackoverflow.com/questions/606191/convert-bytes-to-a-string'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We can also append a topic from linked topics section to the wanted list to get all related &amp;amp; linked topics! Or if we want to get its urls, we can add one of the urls too.&lt;/p&gt;

&lt;h3&gt;
  
  
  Getting exact result
&lt;/h3&gt;

&lt;p&gt;Say we want to scrape live stock prices from yahoo finance:&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;from&lt;/span&gt; &lt;span class="nn"&gt;autoscraper&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;AutoScraper&lt;/span&gt;

&lt;span class="n"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;'https://finance.yahoo.com/quote/AAPL/'&lt;/span&gt;

&lt;span class="n"&gt;wanted_list&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"124.81"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="n"&gt;scraper&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;AutoScraper&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="c1"&gt;# Here we can also pass html content via the html parameter instead of the url (html=html_content)
&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;scraper&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;build&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;wanted_list&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="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You can also pass any custom &lt;code&gt;requests&lt;/code&gt; module parameter. for example you may want to use proxies or custom headers:&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;proxies&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s"&gt;"http"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'http://127.0.0.1:8001'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s"&gt;"https"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'https://127.0.0.1:8001'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;scraper&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;build&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;wanted_list&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;request_args&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;dict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;proxies&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;proxies&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now we can get the price of any symbol:&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;scraper&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get_result_exact&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'https://finance.yahoo.com/quote/MSFT/'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You may want to get other info as well. For example if you want to get market cap too, you can just append it to the wanted list. By using the &lt;code&gt;get_result_exact&lt;/code&gt; method, it will retrieve the data as the same exact order in the wanted list.&lt;/p&gt;

&lt;h3&gt;
  
  
  Saving the model
&lt;/h3&gt;

&lt;p&gt;We can now save the built model to use it later. To save:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Give it a file path
&lt;/span&gt;&lt;span class="n"&gt;scraper&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;save&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'yahoo-finance'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And to load:&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;scraper&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'yahoo-finance'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Generating the scraper python code
&lt;/h3&gt;

&lt;p&gt;We can also generate a stand-alone code for the learned scraper to use it anywhere:&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;code&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;scraper&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;generate_python_code&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="n"&gt;code&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;It will print the generated code. There's a class named &lt;code&gt;GeneratedAutoScraper&lt;/code&gt; which has the methods &lt;code&gt;get_result_similar&lt;/code&gt; and &lt;br&gt;
&lt;code&gt;get_result_exact&lt;/code&gt; which you can use. You can also use &lt;code&gt;get_result&lt;/code&gt; method to get both.&lt;/p&gt;

&lt;h2&gt;
  
  
  Thanks
&lt;/h2&gt;

&lt;p&gt;I hope this project is useful for you and save your time, too.&lt;br&gt;
I look forward to hear any feedback or suggestion.&lt;/p&gt;

</description>
      <category>python</category>
      <category>webscraping</category>
      <category>webdev</category>
      <category>showdev</category>
    </item>
  </channel>
</rss>
