How To Download Any EBook For Free Using Python?

Spread the love

Do you want to have a copy of any ebook for free? In this article, I will show you how you can do this using Python. At the end of this article, I will also share some of the free websites from where you can download any ebook for free.

Any Ebook For Free
Free Ebook Download

How To Download Any EBook For Free?

To download a free ebook using Python, we will use an external Library called libgen-api 1.0.0 which can be installed using pip. I will demonstrate each of its methods with an example for each.

1. Install Necessary Library (In Our Case Libgen)

#install libgen-api
!pip install libgen_api --quiet

# import necessary Libraries
from libgen_api import LibgenSearch
import pandas as pd
pd.set_option("max_colwidth", None)

In the above code, we have installed and imported the Libgen API along with Pandas. Also, please note that I have set the max_colwidth of pandas to None which means “the content of the cell in each column will not be truncated”.

2. Basic Searching : Search By Title Or Author

Note: All queries must be at least 3 characters long. This is to avoid any errors on the LibGen end (different mirrors have different requirements, but a minimum of 3 characters is the official limit).

2.1. Search By Title

# By Title
s = LibgenSearch()
results = s.search_title("Pride and Prejudice")

# As the above output will be in JSON format so create a dataframe for the results
# use json_normalize to create a dataframe using JSON value

result_df = pd.json_normalize(results)
pd.set_option("display.max_rows",result_df.shape[0]+1)
result_df.head(2).T # to transpose the dataframe
Libgen | Search By Title
Libgen | Search By Title

2.2. Seach By Author

# By Author
s = LibgenSearch()
results = s.search_author("Jane Austen")

# As the above output will be in JSON format so create a dataframe for the results
# use json_normalize to create a dataframe using JSON value

result_df = pd.json_normalize(results)
pd.set_option("display.max_rows",result_df.shape[0]+1)
result_df.head(2).T
Libgen | Search By Author
Libgen | Search By Author
Pykit- Libgen
Pykit- Libgen

3. Filtered Searching – To Download Any Ebook For Free

# You can define a set of filters, and then use them to refine the search results.

# By default, filtering will remove results that do not match the filters exactly (case-sensitive) - which can be adjusted by setting exact_match = False when calling one of the filter methods.

# Available fields for filtering-

col_names =

 ["ID",
  "Author",
  "Title",
  "Publisher",
  "Year",
  "Pages",
  "Language",
  "Size",
  "Extension",
  "Mirror_1",
  "Mirror_2",
  "Mirror_3",
  "Mirror_4",
  "Mirror_5",
  "Edit"]

3.1. Filtered Searching By Title – To Download Any Ebook For Free

tf = LibgenSearch()
title_filters = {"Year": "2007", "Extension": "pdf"}
results = tf.search_title_filtered("Pride and Prejudice", title_filters, exact_match=True)
results[0]
Libgen | Filtered Search
Libgen | Filtered Search By Author

3.2. Filtered Searching By Authorr Ebook Fo

af = LibgenSearch()
author_filters = {"Language": "German", "Year": "2009"}
results = af.search_author_filtered("Agatha Christie", author_filters, exact_match=True)
results[0]
Libgen | Filtered Search By Author
Libgen | Filtered Search By Author

3.3. Non-Exact Filtered Searching- (exact_match = False)

ne_af = LibgenSearch()
partial_filters = {"Year": "200"}
results = ne_af.search_author_filtered("Agatha Christie", partial_filters, exact_match=False)
results[0]
Libgen | Non-Exact Filter
Libgen | Non-Exact Filter

Till now, we have extracted the data based on our requirements. The next step is to convert the “Mirror Links” to a downloadable link.

Note: Choose one Book ID at a time. (In my case, I am taking the results obtained from the last search results. For example, ID- 393868 Or simply results[0].

item_to_download = results[0]
download_links = ne_af.resolve_download_links(item_to_download)
links = pd.json_normalize(download_links)
links.T
Libgen | Extract Download Links
Libgen | Extract Download Links

Complete Notebook File

Congratulations! We have successfully extracted the download links for the required ebook. You can see that in the above output, we have a total of 4 links, for example, GET (direct link), Cloudfare, IPFS.io, and Infura. Copy and paste any of the above links to download the ebook for free.

You can also download any ebook or PDF books for free using any of the below-mentioned websites:

  1. Library Genesis
  2. Overdrive
  3. Project Gutenberg
  4. ManyBooks
  5. Open Library

Conclusion

In this article, we have successfully installed the Libgen API and explored its various methods to extract the book’s information. We have also explored the method to convert the Mirror Links to downloadable links using which we can download any ebook for free. In the end, we have seen some of the websites that offer to download and read free ebooks.

Disclaimer: The above information is for education purposes only.

Please support our effort by commenting below. Feel free to contact us in case of any queries.

Check out my other articles:

  1. AI Chatbot in Python (using NLTK): How to build a chatbot?
  2. Learn Data Science: 5 Free Websites For Beginners
  3. Deploying Your Python Flask Application Online: Using Ngrok
  4. How To Build Email Spam Classification Model With 99% Accuracy
  5. How To Use Google Indexing API In WordPress and Blogger

Spread the love
3

Related Posts

© 2022 Pykit - All Rights Reserved!