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
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.
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 APIInteracting 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.
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.
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. Efficiently downloading thousands of images from the internetThis 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:
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.
Google Search automationGoogle 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.
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.
Since the search function generates a generator object, we'll convert it to the list.
When you print the results list, it shows the search results for our keyword.
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 LevelHopefully, 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
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ủ Đề |