In Part 1, we set up Python, PyCharm, and the libraries we’ll be working with. Now that we’re all set up, we can get to the fun part – writing the actual program!
Are you excited?
For the first project, we’re going to create a program that retrieves the search volume of five specific keywords from Google Trends.
As you follow along, you’ll learn:
– How to create a project in PyCharm.
– How to create variables, lists, and even a PNG graph chart from the collected data.
Let’s begin with creating our project folder.
How to create a new Python project in PyCharm
1: Open Pycharm. Open the “File” menu, and select “New Project.”
2: You should now see a popup window that looks like this:

3: Select or create a folder where you would like to save your files and click on “Create.”
NOTE: Make sure that “Virtualenv” is selected as the “New Environment” and that the “Base interpreter” points to the folder path where you’ve installed Python.
5: Right-click on your project folder (in the upper left corner), select “New>Python File,” and give your Python file a name.
Make sure that you select “Python file” in the dropdown menu.

Importing Python Libraries
Before we can use the libraries we imported, we need to import them into our program. This is done with the “import” command, followed by the name of the library.
For example:
import time
– imports the “time” library
We can also be more specific:
from PIL import image
This command tells Python to fetch the sub-library “image,” located inside the library “PIL.”
The libraries we’ll need for our project are:

Further explained – “red wavy lines”:
You might see red wavy lines underneath some of your libraries. Don’t worry; you can solve this by hovering your mouse cursor over the library you’re trying to import.
In the menu that appears, select “Install package” and wait for a few seconds. The wavy lines should disappear.

Next, we’ll create something known as a “variable.”
What is a variable?
In programming, a “variable” is a storage location for data or a specific value given by the coder.
Think of a variable as a big cardboard box with a label on it – when the label is referenced, the program will use the information stored in the box.
In Python, a variable looks like this:
var1 = "Hello World!"
Every time your program references ” var1″, it will fetch the variable’s assigned value (in this case, it’s “Hello World!”)
For example, “print(var1)” will output “Hello World!” to the terminal (since we’ve given the variable ‘var1’ the value of “Hello World”)
For our purpose, we’ll create a variable that stores the Google Trends API. To do that, write the following code in your program:


Further explained – TrendReq parameters:
– “hl” specifies the hosting language for accessing Google Trends. In the code snippet above, it has been set to “English.”
– “tz” specifies the time zone offset. The code snippet above has set the timezone offset to “360”; US Central Standard Time.
– “timeout” specifies how many seconds the program will wait for a connection. In the code snippet above, the timeout is set to 10 seconds.
– “retries” specifies how many times the program will retry to establish a connection. In the code snippet above, it will try twice.
– “backoff_factor” specifies how long the program will wait between connection attempts. In the code snippet above, it will wait for 0.1 seconds.
Consult the Pytrends documentation page for more information.
Next, we’ll create a list to store the keywords we want to search for.
How to Create a List in Python
Lists are similar to variables, except that they can hold multiple values.
Example of a list:
numbered list = [0, 1, 2, 3]
As you can see, lists have square brackets instead of parenthesizes. They can also hold multiple values, separated by commas (“,”)
Our list will look like this (feel free to replace the keywords, as long as you make sure that they are within the double brackets and that the comma is outside of the quotation marks):

Next, we’ll pass these keywords into Google Trends using the ‘build_payload’ function of the ‘pytrends’ library:

Further explained – ‘build_payload’ parameters:
– “kw_list” is the name of the list of keywords we created. Replace this variable with the name of your particular list.
– “cat” specifies which category the search is supposed to be made in. Some examples include:
- TV Commercials: 1055
- Motorcycles: 273
- Fitness 94
In the code snippet above, the search category is set to “Marketing Services.”
For all available “cat” parameters, please refer to the Google Trends Category listing.
– “timeframe” specifies when to start and finish the search. The timeframe in the code snippet above is set to “April 1, 2016” as the starting date, and “April 1st, 2017” as its finishing date.
– “geo” specifies the region. In the code snippet above, it’s set to the default value of “World.”
– “gprop” specifies the Google property to look in. In the code snippet above, it’s set to the default value of “web search.”
Next, we’ll convert the data into a Pandas dataframe:

Create a PNG graph chart and run your program
Almost done – all that remains is to make the data we’ve gathered more readable for the human eye.
We can do this by creating a PNG graph chart from our data, which we’ll do with the following code:

Further explained – cumsum properties:
–“Figsize” sets the width and height of the chart in inches.
– “savefig” is where you name the image. Ensure that the name includes “.png” at the end – otherwise, you might not be able to open the image.
Next, we’ll create a variable for storing the rendered image (make sure that the value inside the brackets match the one you wrote in the “savefig” variable earlier):

Finally, we need to automatically open the image with the default gallery viewer as soon as the script has finished running:

Excellent; we’re all set to go! To run your program, click on the green arrow in the upper right corner of the screen (don’t click on the one that looks like a bug – that one’s for debugging; a completely different task):

After a while, an image should pop up that looks something like this:
NOTE: If your image doesn’t open for some reason, it might be because you haven’t selected a default image viewer. After you’ve done that, you should be just fine.

Conclusion
Congratulations, you’ve just created your very first Python program! Pat yourself on the back for all your hard work.
Now you have something you can use every time you need to see the search volume in Google Trends.
Remember to change the parameters if necessary – such as searching in another category or resizing your chart.
In Part 3, we’ll upgrade the program to accept direct input from the user.
Struggling to explain your technical product or service as straightforward as this blog post?
Would you like to get rid of the confusing fluff and jargon in favor of something that will make your readers go, “Oh, I get it now!” after they’ve read it?
Then, contact me today, and let’s talk some more.
Recent Comments