banner



How To Make Money With Python Automation

Tired of performing repetitive tasks every day? Well, information technology tin can bore even the most resilient of u.s.a. out of our minds. Lucky for united states of america, the digital age we alive in offers us a bevy of tools to relieve ourselves of that sort of deadening labor. One of them is Python - a perfect programming linguistic communication to start your journey with job automation. In this article I will share the reasons to automate tasks with Python and nowadays five ideas with real-life examples.

Table of content:

  1. How to Starting time With Task Automation
  2. Why Employ Python for Chore Automation
  3. What Tin You lot Automate With Python: Step Past Step Guide
    • Reading files

    • Writing files

    • Sending emails

    • Web scraping

    • Interacting with an API

  4. Take Your Python Automation To The Next Level
  5. Further Recommended Read

How to Start With Task Automation?

First of all, I'k here to tell you lot that automation is definitely for you, even if you're a complete newbie to the field. Fifty-fifty though information technology might seem daunting at first, I promise y'all that building your commencement script volition experience very rewarding and your new skills will save you lots of time in the long run. Here's a brief step-by-step guide to begin with:

  1. Commencement past thinking about repetitive tasks your workday entails.
  2. Identify those that y'all call back could be automated.
  3. Divide your workload into smaller sub-tasks.
  4. Retrieve of ways you could automate at least some of them.

In one case you find a suitable task, you lot have to choose the right tool. It shouldn't come up out every bit a surprise that the "tool" I'm going to explore is Python (speaking from a Python developer's perspective) Amid a sheer diversity of languages available, Python is relatively easy to learn and has proven itself useful in a variety of fields.

Why Utilise Python For Job Automation?

Python offers great readability and outgoing syntax. The latter resembles apparently English, which makes information technology an excellent choice to start your journey with. When compared with other languages, Python clearly stands out every bit 1 of the simplest in the bunch. Look at this instance of code written in C++ and Python.

Sample code in C++

Sample code in C++

The same function in Python In Python, the aforementioned functionality took fewer lines written in simpler, friendlier syntax.

The advantages of Python I mentioned above brand the learning process fast and pleasant. With niggling time and effort, you will proceeds enough knowledge to write simple scripts. This smooth learning curve significantly speeds up development, fifty-fifty for experienced developers.

Learning curve for Python vs other programming languages.

Learning curve for Python vs other programming languages

Another thing that may convince you to use Python is that it comes with swell data structure support.

Information structures enable y'all to store and access data, and Python offers many types thereof by default, including lists, dictionaries, tuples, and sets. These structures let you manage information easily, efficiently and, when chosen correctly, increase software performance. Furthermore, the data is stored securely and in a consistent manner.

Even better, Python lets you create your own information structures, which, in turn, makes the linguistic communication very flexible. While data structures may non seem all that important to a newcomer, trust me on this — the deeper you go, the more of import your selection of information structure tends to get.

Python's data structure

Python's data structure

You lot can automate nearly everything with Python. From sending emails and filling out PDFs and CSVs (if you lot are non familiar with this file format I advise to bank check it, it'south for example used past Excel) to interacting with external APIs and sending HTTP requests. Whatsoever your thought, it's more than likely that you can pull it off using Python along with its modules and tools.

Tons of libraries created for Python make the linguistic communication really powerful, assuasive developers to tackle everything from machine learning and web scraping to managing your figurer's operating system.

Where Python finds its use

Where Python finds its use

Python's strengths likewise include a decent support structure and a large community of enthusiasts. The language continues to abound in popularity and manufactures roofing basically all of the concepts underpinning the linguistic communication continue popping upwards on the Spider web — a cursory search is bound to yield some pretty interesting blog or StackOverflow posts, and if it doesn't, y'all can always mail service a question or problem yous take to whatsoever one of the Python forums effectually the Web. Trust me, you won't stay alone with your problem for long.

Python has a great community around it and the language itself is in abiding development. Plus, in that location are new third-party libraries showing up all the fourth dimension.

Far from a darling of the software development community, Python has institute apply across a number of professions and industries, including science, data analysis, mathematics, networking, and more.

New call-to-action

What Can You Automate With Python?

Almost everything! With a little chip of work, basically whatever repetitive task can be automatic.

To practice that, you lot only need Python on your computer (all of the examples hither were written in Python 3.7) and the libraries for a given trouble. I'thou not going to teach you lot Python, but show that automation is easy with it. In the examples below, I used iPython, which is a tool that helps to write the lawmaking interactively, pace by stride.

For simple automation, Python's built-in libraries should be enough. In other cases, I will let y'all know what should be installed.

Reading and writing files

Reading and writing files is a task that y'all tin efficiently automate using Python. To showtime, you but need to know the location of the files in your filesystem, their names, and which mode you should use to open up them.

In the case below, I used the with statement to open up a file — an arroyo I highly recommend. Once the with block lawmaking is finished, the file is closed automatically and the cleanup is done for the states. You can read more than about it in the official documentation.

Let'due south load the file using the open() method. Open up() takes a file path as the first argument and opening mode every bit the second. The file is loaded in read-only style ('r') past default. To read the entire content of a file, utilise the read() method.

                              In [1]: with open("text_file.txt") as f:    ...:     impress(f.read())                    ...:       A simple text file. With few lines. And few words.                          

To read the content line by line, effort the readlines() method — it saves the contents to a list.

                              In [2]: with open("text_file.txt") as f:    ...:     impress(f.readlines())                    ...:   ["A simple text file.\north",  "With few lines.\north", "And few words.\n"]                          

Y'all tin can also modify the contents of a file. One of the options for doing so is loading information technology in write ('w') mode. The mode is selected via the 2nd argument of the open() method. But exist careful with that, as it overwrites the original content!

                              In [3]: with open("text_file.txt", "w") as f:     ...:     f.write("Some content")               ...:  In [iv]: with open up("text_file.txt") as f:     ...:     impress(f.read())               ...:  Some content                          

One peachy solution is to open up the file in append ('a') mode, which means that new content will be appended to the end of the file, leaving the original content untouched.

                              In [5]: with open("text_file.txt", "a") as f:     ...:     f.write("\nAnother line of content")               ...:  In [6]: with open("text_file.txt") every bit f:     ...:     print(f.read())               ...:  Some content Another line of content                          

As you can see, reading and writing files is super easy with Python. Feel gratis to read more virtually the topic, especially the modes of opening files because they tin be mixed and extended! Combining writing to a file with Web scraping or interacting with APIs provides yous with lots of automating possibilities! As a next step you could also check a bang-up library csv which helps with reading and writing CSV files.

Sending emails

Another task that tin exist automatic with Python is sending emails. Python comes arranged with the not bad smtplib library, which yous tin employ to send emails via the Simple Mail Transfer Protocol (SMTP). Read on to see how uncomplicated information technology is to transport an email using the library and Gmail'due south SMTP server. Yous volition demand an email business relationship in Gmail, naturally, and I strongly recommend y'all create a separate business relationship for the purpose of this script. Why? Because yous'll need to set the Allow less secure apps option to ON, and this makes it easier for others to gain access to your private data. Set up the account now and let'south jump into lawmaking in one case you're done.

First of all, nosotros volition need to establish an SMTP connection.

              In [one]: import getpass                                                                                              In [ii]: import smtplib                                                                         In [three]: HOST = "smtp.gmail.com"                                                                                      In [4]: PORT = 465 In [v]: username = "username@gmail.com"                                                                              In [6]: countersign = getpass.getpass("Provide Gmail password: ") Provide Gmail password: In [vii]: server = smtplib.SMTP_SSL(HOST, PORT)                          

The requisite, congenital-in modules are imported at the get-go of the file, we utilize getpass to deeply prompt for the password and smtplib to found a connection and send emails. In the post-obit steps, the variables are set up. HOST and PORT are both required by Gmail — they're the constants, which is why they're written in uppercase.

Adjacent, you provide your Gmail account name that will be stored in the username variable and blazon in the password. It'due south good practice to input the password using the getpass module. Information technology prompts the user for a countersign and does not echo it dorsum later on you lot blazon information technology in. Then, the script starts a secure SMTP connexion, using the SMTP_SSL() method. The SMTP object is stored in the server variable.

              In [8]: server.login(username, password)                                Out[8]: (235, b'ii.7.0 Accepted') In [9]: server.sendmail(   ...:     "from@domain.com",          ...:      "to@domain.com",   ...:     "An e-mail from Python!",   ...:     ) Out[9]: {} In [8]: server.quit()                           Out[8]: (221, b'2.0.0 endmost connexion s1sm24313728ljc.3 - gsmtp')                          

Finally, y'all authenticate yourself using the login() method and… that's information technology! From now on, you volition exist able to send emails with the sendmail() method. Please remember to clean up later on, using the quit() method.

Web scraping

Spider web scraping allows you to extract data from Web pages and save it on your difficult bulldoze. Imagine your workday involves pulling information from a website y'all visit every twenty-four hour period. Scraping could be of much aid in such a instance, every bit once code is written information technology tin can be run many times, making it especially useful when handling large amounts of data. Extracting information manually takes a lot of time and a lot of clicking and searching.

With Python, information technology couldn't be easier to scrape data from the Web. But in order to clarify and extract information from HTML code, the target page has to be downloaded beginning. The requests library will exercise the job for us, but you need to install it first. Simply type the following in your console:

              pip install requests            

(for more details, cheque the official documentation: https://requests.readthedocs.io/en/master/user/install/#install)

With the page downloaded, we tin can now excerpt the actual data we want. This is where BeautifulSoup comes in. The library helps with parsing and pulling data from structured files. Naturally, the library also has to be installed first. Like earlier, type the following in your console:

              pip install beautifulsoup4            

(for more details, cheque the official documentation)

Let's run through a rather uncomplicated example to see how the automation bit works here. The HTML code of a webpage we selected for parsing is really cursory, and pocket-size wonder, given that its purpose is to testify what week of the year information technology is. Come across information technology here: What Calendar week Is It. To inspect the HTML lawmaking, simply right-click anywhere on the page and choose View folio source.
So run the interactive Python (by simply typing ipython in the console) and allow'south commencement fetching the page using requests:

                              In [one]: import requests                                                                                              In [2]: response = requests.get("https://whatweekisit.com/")                                                        In [3]: response.status_code Out[3]: 200                          

With that washed, the folio is then downloaded and stored in a response variable. If you want to come across its contents, blazon response.content in the interactive final. The HTTP status code 200 tells us that the request succeeded.

Now it's time for BeautifulSoup to exercise its task. Nosotros start with importing the library and then creating a BeautifulSoup object called soup. The soup object is created with the fetched information as an input. We too let the library know which parser should be utilized, with html.parser for HTML pages, plainly.

                              In [4]: from bs4 import BeautifulSoup                           In [5]: soup = BeautifulSoup(response.content, "html.parser")   In [6]: soup                                                                       Out[6]:   <html><head><meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>                          

The HTML document is now saved in the soup object. It'due south represented as a nested structure (its fragment is printed above). There are several ways to navigate through the structure. A few of them are shown below.

              In [7]: soup.title                                    Out[seven]: <title>What calendar week of the yr is it?</championship> In [viii]: soup.title.string                             Out[8]: 'What week of the year is information technology?' In [9]: soup.find_all("p") Out[ix]:  [<p>This unmarried serving app calculates the calendar week of the twelvemonth and twenty-four hour period of the year for the current 24-hour interval and for any day which y'all specify.  Select a engagement from the agenda to see this in activity.</p>,  <p>Please note that this site uses the ISO week date system. This is used primarily for fiscal and government timekeeping. For mor east information, delight refer to this <a href="https://en.wikipedia.org/wiki/ISO_week_date" target="_blank">Wikipedia commodity</a>.</p>]                          

Y'all tin can easily extract the championship of the page or notice all the <p> tags in the data. The all-time way to get a feeling for information technology is to fiddle with the object yourself.

Permit'due south attempt to extract the data nosotros wanted at the very beginning. What week of the yr is it? Studying the HTML lawmaking, we volition see that the information is subconscious in a table, under the <tabular array> tag. We can extract the tabular array from the soup object and save it in a variable using notice(). With the table saved, it's actually piece of cake to get all the <td> tags which shop the information. Invoking find_all() on table_content returns a list of <td> tags. And to print them in a squeamish-looking format, simply iterate over the list and get_text() from each detail.

              In [x]: tabular array = soup.find("table") In [11]: table Out[11]:  <table edge="0" class="box"> <tbody><tr><th colspan="two">Current date info</th></tr> <tr><td>Today's date is:</td><td><b>Wednesday, April 15th, 2020</b></td></tr> <tr><td>Week of the yr:</td><td><b>16 of 53</b></td></tr> <tr><td>Day of the year:</td><td><b>106 of 366</b></td></tr> </tbody></tabular array>  In [12]: table_content = table.find_all("td")         In [13]: for tag in table_content:      ...:     print(tag.get_text())                       ...:     Today's appointment is: Wednesday, Apr 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 folio using just a few commands. I strongly encourage you to read more about the library! Information technology's really powerful, especially when working with larger and more than 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 Spider web.

There are multiple APIs bachelor, simply the Open AQ Platform API seems the nicest choice, generally because it does not require authentication (the relevant documentation can be found hither: 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 information technology in the previous example.

                              In [1]: import requests In [two]: response = requests.get("https://api.openaq.org/v1/measurements?metropolis=Paris&parameter=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 withal you wish—simply refer to the API documentation if y'all desire to dive a petty deeper into the matter.

The script then stored the pulled information in key-value JSON format, which is cleaner and improves readability. It was achieved cheers to the json() method invoked on the response object. Yous can see a clamper of the response below.

              In [5]: response_json                                               Out[five]:  {'meta': {'name': 'openaq-api',   'license': 'CC Past 4.0',   'website': 'https://docs.openaq.org/',   'page': 1,   'limit': 100,   'establish': 53396},  'results': [{'location': 'Paris',    'parameter': 'pm25',    'date': {'utc': '2020-05-05T09:00:00.000Z',     'local': '2020-05-05T04:00:00+02:00'},    'value': 17.two,    '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 tin can become the most recent value by accessing the first chemical 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',  'engagement': {'utc': '2020-05-05T04:00:00.000Z',   'local': '2020-05-05T06:00:00+02:00'},  'value': 17.2,  'unit': 'µg/1000³',  'coordinates': {'breadth': 48.8386033565984, 'longitude': 2.41278502161662},  'country': 'FR',  'urban center': 'Paris'}                          

P.Due south Python is much more automation. See how we helped Avisio deliver the MVP in 4 months. Read the whole case study beneath: Avision case study - Python project

Take Your Python Automation To The Next Level

Hopefully, after spending a few minutes of your life reading this article, y'all will realize that tons of tasks your daily life involves can be hands automated abroad, even without broad programming knowledge.

If that'south not enough for you and y'all feel like creating some more automation, there are multiple sources on the Web that will offering you lot a deeper dive into the bailiwick.

One book I strongly recommend is Al Sweigart'southward Automate the Boring Stuff with Python: Applied Programming for Total Beginners. It provides a swell set up of automation examples with a hint of theory behind them. It will requite 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 Programmer in Monterail
  4. Django vs Node.js: When to Choose Which Framework
  5. Flask vs Django – Which Python Framework To Cull and When?
  6. 5 Reasons to Choose Python for Finance and Fintech
  7. Python for Mobile App Development – Is It a Good Pick in 2022?
  8. Why Is Python Used for Machine Learning?
  9. Is Python Slow?

New call-to-action

Source: https://www.monterail.com/blog/python-task-automation-examples

Posted by: coreyittly1942.blogspot.com

0 Response to "How To Make Money With Python Automation"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel