Is python good for automating tasks?

Tired of performing repetitive tasks every day? Well, it can bore even the most resilient of us out of our minds. Lucky for us, the digital age we live in offers us a bevy of tools to relieve ourselves of that sort of tedious labor. One of them is Python - a perfect programming language to start your journey with task automation.

In this article, we will share the reasons to automate tasks with Python and present six ideas with real-life examples. The first four Python automation examples are by me, and the two last, by Arzu Huseynov.

While these task automation examples are simple, they can work as a foundation if you would like to build automating Python scripts to fully perform IT automation with Python.

Wikipedia article.

]

You can easily extract the title of the page or find all the

tags in the data. The best way to get a feeling for it is to fiddle with the object yourself.

Let’s try to extract the information we wanted at the very beginning. What week of the year is it? Studying the HTML code, we will see that the information is hidden in a table, under the

tag. We can extract the table from the soup object and save it in a variable using find[].

With the table saved, it's really easy to get all the

tags that store the information. Invoking find_all[] on table_content returns a list of tags.

And to print them in a nice-looking format, simply iterate over the list and get_text[] from each item.

In [10]: table = soup.find["table"]
In [11]: table
Out[11]: 
Current date info
Today's date is:Wednesday, April 15th, 2020
Week of the year:16 of 53
Day of the year:106 of 366
In [12]: table_content = table.find_all["td"] In [13]: for tag in table_content: ...: print[tag.get_text[]] ...: Today's date is: Wednesday, April 15th, 2020 Week of the year: 16 of 53 Day of the year: 106 of 366

With help from the marvelous BeautifulSoup library and a few straightforward steps, we were able to extract interesting content from the page using just a few commands. I strongly encourage you to read more about the library! It’s really powerful, especially when working with larger and more nested HTML documents.

Interacting with an API

Interacting with APIs gives you superpowers! For a simple example of this particular application, let’s try to pull air quality data updates from the Web.

There are multiple APIs available, but the Open AQ Platform API seems the nicest option, mostly because it does not require authentication [the relevant documentation can be found here: Open AQ Platform API]. When queried, the API provides air quality data for the given location.

I used the requests library to fetch the data, the same way we did it in the previous example.


In [1]: import requests
In [2]: response = requests.get["//api.openaq.org/v1/measurements?city=Paris¶meter=pm25"]
In [3]: response.status_code
Out[3]: 200
In [4]: response_json = response.json[] 

The code above pulled air quality data for Paris, looking only for the PM25 value. You can customize the search however you wish—simply refer to the API documentation if you want to dive a little deeper into the matter.

The script then stored the pulled data in key-value JSON format, which is cleaner and improves readability. It was achieved thanks to the json[] method invoked on the response object. You can see a chunk of the response below.

In [5]: response_json                                              
Out[5]: 
{'meta': {'name': 'openaq-api',
  'license': 'CC BY 4.0',
  'website': '//docs.openaq.org/',
  'page': 1,
  'limit': 100,
  'found': 53396},
 'results': [{'location': 'Paris',
   'parameter': 'pm25',
   'date': {'utc': '2020-05-05T09:00:00.000Z',
    'local': '2020-05-05T04:00:00+02:00'},
   'value': 17.2,
   'unit': 'µg/m³',
 'coordinates': {'latitude': 48.8386033565984,   'longitude': 2.41278502161662},
   'country': 'FR',
   'city': 'Paris'},

The exact values pulled are hidden under the results key, with the latest pulls sitting near the top of the list, meaning that we can get the most recent value by accessing the first element of the list with index zero. The code below gets us the PM25 concentration in the air in Paris for May 5, 2020.

In [6]: response_json["results"][0]                                 
Out[6]: 
{'location': 'FR04329',
 'parameter': 'pm25',
 'date': {'utc': '2020-05-05T04:00:00.000Z',
  'local': '2020-05-05T06:00:00+02:00'},
 'value': 17.2,
 'unit': 'µg/m³',
 'coordinates': {'latitude': 48.8386033565984, 'longitude': 2.41278502161662},
 'country': 'FR',
 'city': 'Paris'}

Efficiently downloading thousands of images from the internet

This example and the next were kindly provided by our senior Python developer, Arzu Huseynov. 

A couple of lines of Python code can help you to automate this huge task in a matter of minutes thanks to the Python community and the creators of this language. A more simplistic approach will help you to download one image at a time. But with help of the Multithreading concept, you can download images parallelly. This saves you a lot of time.

Let’s start out by importing the following libraries:

  • uuid - This is a built-in library to generate random uuid values. We will use these random values in our program to generate image names. Since we don’t need to override images with duplicate names, we need to make sure that our image names are unique.
  • requests - You can simply download the requests library from PyPI. Downloading libraries is not in the scope of this blog post but you can find details on here.
  • concurrent.futures - With this built-in library, we’ll use the threading pool functionality of Python.
from typing import List
import uuid
import requests
import concurrent.futures
Next step is to create a list of the image sources. 
urls: List = [
   “/image.jpg”,
   “/image_2.jpg”,
]

Next, we create a simple python function. This function will download the images, generate new names and save them.

Additionally, for every successful job, it will print a message, which is useful for logging.

Additionally, for every successful job, it will print a message, which is useful for logging. 

def save_image[url: str] -> None:
   img_content = requests.get[url].content
   img_name = f'img_{str[uuid.uuid4[]]}.jpg'
   with open[img_name, 'wb'] as img_file:
       img_file.write[img_content]
       print[f'{img_name} was downloaded...']
with concurrent.futures.ThreadPoolExecutor[] as executor: for url in urls: executor.submit[save_image, url]


As mentioned before, we used multithreading, which exponentially saves time the more images we work with. Learn more why: Multiprocessing vs Multithreading in Python – Explained With Cooking

Google Search automation

Google search is probably something that we all use every day. If you have to deal with some repetitive searches like checking your company's SEO performance, collecting data, etc, the googlesearch-python library can help you to achieve your goals.

In the program below, we’ll look for up to 10 exact matches of “Python Development Monterail” in a set of pages.

Let’s start with importing the library.

from googlesearch import search

Next, we use the search function of the googlesearch library. As you can see, we can customize the results with num_results and lang arguments.

results = search[term="Python Development Monterail", num_results=10, lang="en"]

Since the search function generates a generator object, we'll convert it to the list.

results = list[results]

When you print the results list, it shows the search results for our keyword. 

print[results]

[
   "/services/python-development-services",
   "/careers/python-developer-wroclaw",
   "/blog/topic/python",
   "/blog/what-its-like-to-be-a-python-developer-in-monterail",
   "/blog/python-for-mobile-app-development",
   "//apply.workable.com/monterail/j/F5F19D831D/",
   "//bulldogjob.pl/companies/jobs/81190-senior-python-developer-wroclaw-monterail",
   "//pl.linkedin.com/in/artur-pietracha-b216b641",
   "//www.facebook.com/monterail",
   "//fi.talent.com/view?id=1abad4456af7",
] 

This can save you time especially if you constantly search for exactly the same keywords on google. With a little bit of creativity and enthusiasm, your boring daily stuff can be really fun.

P.S Python is much more than automation. See how we helped Avisio deliver the MVP in 4 months. Read the whole case study below:

Take Your Python Automation To The Next Level

Hopefully, after spending a few minutes of your life reading this article, you will realize that tons of tasks your daily life involves can be easily automated away, even without broad programming knowledge.

If that's not enough for you and you feel like creating some more automation, there are multiple sources on the Web that will offer you a deeper dive into the subject.

One book I strongly recommend is Al Sweigart's Automate the Boring Stuff with Python: Practical Programming for Total Beginners. It provides a great set of automation examples with a hint of theory behind them. It will give you a much deeper view of Python’s capabilities and improve your knowledge of the language at the same time.

And remember—never spend precious time on repetitive, tedious tasks that can be easily automated!

Further Recommended Read on Python

  1. What Is Python and Why Is It so Popular?
  2. When to Use Python and How Can It Benefit Your Business?
  3. What It’s Like To Be a Python Developer in Monterail
  4. Django vs Node.js: When to Choose Which Framework
  5. Flask vs Django – Which Python Framework To Choose and When?
  6. Five Reasons to Choose Python for Finance and Fintech
  7. Python for Mobile App Development – Is It a Good Choice in 2022?
  8. Why Is Python Used for Machine Learning?
  9. Is Python Slow?

Is Python a good language for automation?

Python is an ideal option for automation because it is a server-side, scripting language. This makes it an ideal language for selenium automation testing. The popular language has a wide range of libraries and frameworks. Consider using automation tools like Pywinauto, Behave, Robot frameworks, and Selenium.

What is the best programming language for automating tasks?

Automating Tasks With Python There are a lot of programming languages around today but python is by far the most popular and preferred language to anyone who seeks to learn the art of task automation.

Chủ Đề