To send a message to a Telegram channel using Python, first of all it is required to create a Telegram bot. While creating the Telegram bot you will get an apiToken and a chatID that will be used in a Python script to access the Telegram API and send the message. In this note you […]
python
Python: Write to File | Read from File – Best Practice
To write something to a file or to read the content of a file in Python it is required to open() and close() the file properly. The best practice in Python is to read/write to a file by using the with statement – the replacement for commonly used try…finally error-handling statements. The with statement ensures […]
Run Selenium Headless – Chrome | Firefox (Python)
Selenium is an open source tool for web browsers automation that is widely used in tandem with Python to test web applications. As these tests are often automated and run on dedicated nodes, there is a sense to run Selenium in a headless mode, i.e. without opening a browser. This note shows how to run […]
Click Button by Text – Python & Selenium
Selenium is an open source tool that automates web browsers and is widely used for automated testing of web applications. With Python and Selenium you can easily create a script that searches a web-page for a button containing a particular text and clicks on it. This note shows an example of how to find a […]
Python Script Not Showing Output [SOLVED]
It may happen that you execute a Python script and nothing happenes, i.e. it seems that it “hangs” without showing any output. Even if you explicitly include some print() statements to debug the issue, it may still not print anything to a terminal. One of the reasons of why your Python script does not show […]
Python: Check Package Version
While working with Python you may wonder what is the version of a certain Python package (library) that you are using. There are several ways of how to get the version of a package or module in Python. This note describes how to check the version of Python packages from a Python shell, from a […]
Python: Login to Website – Selenium
Selenium is an open source tool that automates web browsers and is widely used for automated testing of web applications. With Python and Selenium you can easily create a script that automates login to a website, that is very useful, for example, for web scraping. This note shows how to create a Python script to […]
Python: Sleep Random Time – Web Scraping
Some websites can block access to prevent web scraping, that can be easily detected if your Python script is sending multiple requests in a short period of time. To not get banned you can try to add random delays between queries. For this you can use the Python’s sleep() function that suspends (waits) execution of […]
Python: Download File from URL & Save
A Python can be used to download a text or a binary data from a URL by reading the response of a urllib.request.urlopen. The downloaded data can be stored as a variable and/or saved to a local drive as a file. Below you will find the examples of the Python code snippets for downloading the […]
Python Requests ‘User-Agent’ – Web Scraping
A ‘User-Agent’ HTTP request header is a string that a web browser is sending to a web server along with each request to identify itself. The ‘User-Agent’ string contains information about which browser is being used, what version and on which operating system. Some websites block access from non-web browser ‘User-Agents’ to prevent web scraping, […]