Lets install the IDE first. You can use whatever you like – wind IDE, Ninja etc but here we are using Geany. It is a text editor – light and fast and has some basic features of an IDE.
You can get it from www.geany.org
TASK 1 – Open a page in a web browser
We are going to use webbrowser module for this.
Save this file as task1.py and open it as below from cmd. The path of python may be different in your computer. Use cd .. and dir commands to find the python path in case you are not sure of it, it will be somewhere in program files (x86)
TASK 2 – Downloading files from the web
Here we will be downloading a text file and then printing its first 1000 characters. Here I have limited my scope to text files alone.
The path mentioned in the code will lead to the below text page. (just fro reference)
Below is the output when we run the file.
TASK 3 – HTML parsing
Lets use this wiki page about wiki itself a target.
The below code will request for the page and will create an object out of it. Now we can search the object for elements.
Change the code to get the 11th div element
TASK 4 – GOOGLING
If I have to search something I will search for the word and then open the top 5 google results in separate tabs so that i can refer them easily for which one is important and servers the purpose.
The below code uses requests module to open the browser, sys module to join or fit your search item to the URL for google search and bs4 module to parse the page.
The code flow is as below.
- Opens a browser
- Join the search string with the URL
- Create a beautiful soup(bs4) object for the same
- select the results with .r a element in that (this contains the destination location)
- Loop through the elements(first five) and open the destination URL(href) one by one each will open in a separate tab.
TASK 5 – Controlling the browser
An easier way to control the browser is by using selenium module. It can be used to interact with the sites , fill forms and much more. I have searched for saavn.com which is a site to search songs. Let’s inspect the search element, You can see that the element id of the search bar is search, take a note : we will use this info in the code.
First 3 lines are self explanatory, to open firefox browser with the desired website. Now we are finding the element with element id search. send_keys function will type the string in that element and .submit will send the request.
TASK 6 – 1 WAY CHAT
We will use the socket module of python to get it done. this is one of the coolest module and integrates networking concepts with coding aspect of it.
The below code will set up a TCP connection on port 50000 and the socket object i.e. sock is listening, we are ready to accept the connection now.
Open a second CMD screen and try to connect to the above receiver which is listening and is ready to accept the connection.(Use the same PORT)
As soon as you type the above code , screen 1 will change slightly , refer below (highlighted >>>)
Now from the sending screen (screen 2), we are sending a message
Now on the receiver we have received the connection with 1024 buffer size.
TASK 7 – Hypnotizing square
pyautogui is the module to handle mouse and keyboard through python. time module is used to either speed up or delay the execution time.
Code flow :
- Introduce a delay of 5 seconds ; I will explain later why we need it.
- .click function will left click the mouse but will not release it.
- now we are dragging(relational drag) the mouse to a distance of 200 in a time frame of 2 sec.
- reduce the distance and loop it to get a square spiral.
Now 2 things to ponder
- We have introduced the time lag of 5 sec so that you can open paint in your laptop and select the pencil so that u are able to see the spiral, else the mouse will keep on moving in the normal screen and you will not be able to see the spiral.
- The coordinates of the screen are similar to the 4th quadrant of a maths graph.
- Top left corner(0,0)
- Top right corner (value,0)
- Bottom left corner (0,-value)
- Bottom right corner (value,-value)