Download Multiple Files On Web Page Using Requests And BeautifulSoup

Ever found yourself scrolling through a web page filled with multiple files, feeling overwhelmed with the tedious process of clicking on each one individually to download them? Wouldn’t it be great to automate this task, saving both time and effort? I had a similar requirement where I needed to download a lot of PDFs on a single web page, and clicking on each, waiting for the PDF to download, and then waiting for the browser to become responsive again was all becoming a tedious task. ...

April 2, 2023 · 6 min · 1138 words · Ryan Sheehy

Can You Download Python for Free? Where and How?

If you are interested in learning Python programming language, you might be wondering whether you can download it for free. The answer is yes, you can download Python for free. Python is an open-source programming language, which means that its source code is freely available and can be modified and distributed by anyone. Python can be downloaded for free from the official Python website, which provides installers for Windows, macOS, and Linux operating systems. The website also provides detailed instructions on how to install Python on your computer. Additionally, many Linux distributions come with Python pre-installed. ...

March 28, 2023 · 4 min · 730 words · Ryan Sheehy

Download Files Using Python Requests Library

If you’re looking to download a file using Python, the requests library is a great option to consider. This library allows you to easily make HTTP requests and handle responses in a Pythonic way. First, you’ll need to install the requests library if you haven’t already. You can do this using pip, the package installer for Python. pip install requests Once you have requests installed, you can use it to make a GET request to the URL of the file you want to download. This will return a Response object that contains the content of the response, including the file you want to download. ...

March 28, 2023 · 5 min · 936 words · Ryan Sheehy

Copy List Using [:] In Python

The empty slice operator [:] in Python is a powerful and concise way of copying a list. It is shorthand syntax allowing you to create a new list that contains all the elements of the existing list where the operator is used. This operator is represented by one colon wrapped in square brackets [:] with no values or spaces inside. While the empty slice operator may seem like a simple and straightforward way to copy a list, it is important to note that it only creates a shallow copy. This means that if the list contains mutable objects, such as other lists or dictionaries, the new list will still reference the objects contained in the original list. Therefore, any changes made to the original objects will also be reflected in the new copied list. ...

March 25, 2023 · 4 min · 706 words · Ryan Sheehy

Why Does list1 = list2 Not Create A Copy In Python

When starting out in Python it can be easy to think that the expression list2 = list1 will make list2 contain a copy of list1. But it doesn’t take long to realise that this doesn’t meet expectations of what actually happens in the wild. Take the following code as an example: 1 2 3 4 5 6 7 >>> list1 = [1, 2, 3, 4, 5] >>> list2 = list1 >>> list1.append(6) >>> print(list1) [1, 2, 3, 4, 5, 6] >>> print(list2) [1, 2, 3, 4, 5, 6] Huh? Why did list2 contain the same contents as list1 even when the insertion of additional elements in list1 happened after list2 had already been assigned? ...

March 24, 2023 · 9 min · 1715 words · Ryan Sheehy

id() Function in Python: What’s It Good For?

The id() function in Python is a built-in function that returns the unique identity of an object. This identity is an integer that is guaranteed to be unique and constant for the object during its lifetime. The id() function can be used to determine whether two variables refer to the same object in memory. This is helpful when determining the type of copy you have with two variables: if they refer to the same id() number then they are referencing the same object, on the other hand if they refer to two distinct id() numbers then while the objects may contain the same content they are different objects. ...

March 19, 2023 · 6 min · 1246 words · Ryan Sheehy

Sort List By Second (or Nth) Element In Python: 1 Line Of Code

How do you sort a list of lists by the second element using Python? To sort a list of lists by the second element in each list use the key parameter to either the .sort() list function (if modifying the original list) or the sorted() function (if returning a new list). Here’s an example demonstrating how you can do this with code: 1 2 3 4 >>> list_2d = [['2nd', 2], ['3rd', 3], ['1st', 1]] >>> list_2d.sort(key=lambda x: x[1]) >>> print(list_2d) [['1st', 1], ['2nd', 2], ['3rd', 3]] As you can see from the above code the key parameter in the .sort() list function uses a lambda expression which takes each list and extracts the second element. This is used as the key to determine how each list will be sorted. ...

November 5, 2022 · 2 min · 320 words · Ryan Sheehy

How To Remove From String In Python

How do you remove a newline character \n from a string in Python? The easiest way to remove a new line from a string in Python is by using the string method .replace(). Another way is to use the string method .strip() if the new line is at the start or end of the string. Here are some examples demonstrating how to remove the new line characters: 1 2 3 4 5 6 7 8 9 10 11 >>> s = """ ... This string has new lines ... everywhere! ... """ >>> print(s) This string has new lines everywhere! >>> s.replace('\n', '') 'This string has new lineseverywhere!' As you can see from the above output the string .replace() method takes two parameters: the first being the string to find and the second being the string to replace once found. ...

September 21, 2022 · 6 min · 1236 words · Ryan Sheehy

Close Python In Terminal: Command & Shortcut

How do you quit out of Python when you’re in an interactive shell environment? The easiest way to close the Python shell in Terminal is to issue the quit() command. Here’s an example demonstrating how to exit from a terminal window using the a-Shell app on the iPhone: As you can see from the example above I was able to access the Python interactive shell by entering python3 on the command line. This simple instruction invokes the Python REPL and enables the user to enter Python commands. ...

September 20, 2022 · 2 min · 250 words · Ryan Sheehy

Python: Move Files From One Directory To Another

How do you move files from one directory to another using Python? If you want to move specific files from one directory to another with Python, use the glob library to fetch the correct files, then use the os library’s rename method to change the current file to the new directory. Here is an example of how this might look using a Python script. 1 2 3 4 5 import os import glob current_dir = os.getcwd() old_files = os.path.join(current_dir, 'old_loc', '*.py') With the current code I have imported the 2 essential libraries needed to help co-ordinate the change, these being the os and glob libraries. ...

September 20, 2022 · 4 min · 799 words · Ryan Sheehy