Trying to fix this python script

Joined
Jan 18, 2023
Messages
3
Reaction score
0
hello everyone,happy new year.

I have a problem with this this python scrip, is for download ebook from Pdfdrive
i can't make it work , everytime I trying to download
appear this error, >>>> "list index out of range"
can you help to fix this error.

how this scrip work :

run Test1.py in termux


1. select download
search >>>> "name of the book you wanna download"
>>>> enter de number you wanna download
>>>>> the error is this "list index out of range"

I need to fix this "list index out of range"

video Test1.py


Test1.jpg


Test1(1).jpg



thanks very much.
 
Joined
Jan 30, 2023
Messages
108
Reaction score
13
The error "list index out of range" is likely happening because you are trying to access an index of a list that is not present. Here are a few ways to fix this:

  1. Check if the list is empty or not before accessing its elements. If the list is empty, handle the case appropriately.
  2. Use the try-except block to handle the error. In the except block, you can either return an error message or use a default value.
  3. Increase the size of the list.
Without the complete traceback, it's difficult to determine exactly where the error is occurring and what needs to be done to fix it. You may need to do some debugging to find the exact line of code that is causing the problem.

Python:
import concurrent.futures as fu
import sys
import requests as req
import os

try:
    from bs4 import BeautifulSoup as Soup
except ImportError:
    os.system('pip install bs4')
    from bs4 import BeautifulSoup as Soup
try:
    from tqdm import tqdm
except ImportError:
    os.system('pip install tqdm')
    from tqdm import tqdm


PREFIX = 'https://www.pdfdrive.com/'
DIR_LINK = 'https://www.pdfdrive.com/download.pdf?id={}&h={}&u=cache&ext=pdf'
BASE = 'Pdf Drive/'

SUGGEST = 'https://www.pdfdrive.com/search/complete?'

# seperated by -
SUGGESTED_FIRST_PAGE = 'https://www.pdfdrive.com/{}-books.html'
SUGGESTED_OTHER_PAGES = 'https://www.pdfdrive.com/search?q={}&pagecount=&pubyear=&searchin=&page={}'

# seperated by +
NOT_SUGGESTED_FIRST_PAGE = 'https://www.pdfdrive.com/search?q={}&pagecount=&pubyear=&searchin=&em=&more=true'
# seperated by %20
NOT_SUGGESTED_OTHER_PAGES = 'https://www.pdfdrive.com/search?q={}&pagecount=&pubyear=&searchin=&em=&more=true&page={}'


def suggest(search):

    parts = search.split()
    if len(parts) != 1:
        temp = ' '.join(parts[:-1]) + ' ' + parts[-1][:-2]
    else:
        temp = parts[0][:-1]

    params = {
        'query': temp,
    }

    r = req.get(SUGGEST, params=params)

    suggestions = r.json()['suggestions']
    if search in suggestions:
        return True, search
    else:
        while True:
            for index, each in enumerate(suggestions):
                print(index, '-', each)
            try:
                x = int(input("-1 - just my search\n-2 - search again \nSuggest ->"))
                if x == -1:
                    return False, search
                if x == -2:
                    return suggest(input("Search > "))

                if x < 0 or x >= len(suggestions):
                    print('Index Error')
                    continue
                return True, suggestions[x]
            except Exception as e:
                print(e)
                continue


def canGoForward(soup):
    try:
        pagination = soup.find('div', class_='Zebra_Pagination')
        if pagination is not None:
            next_page = pagination.find_all('li')[-1]
            if next_page.text == 'Next':
                return next_page.a['href'] != 'javascript:void(0)'

Try this
 
Joined
Jan 18, 2023
Messages
3
Reaction score
0
The error "list index out of range" is likely happening because you are trying to access an index of a list that is not present. Here are a few ways to fix this:

  1. Check if the list is empty or not before accessing its elements. If the list is empty, handle the case appropriately.
  2. Use the try-except block to handle the error. In the except block, you can either return an error message or use a default value.
  3. Increase the size of the list.
Without the complete traceback, it's difficult to determine exactly where the error is occurring and what needs to be done to fix it. You may need to do some debugging to find the exact line of code that is causing the problem.

Python:
import concurrent.futures as fu
import sys
import requests as req
import os

try:
    from bs4 import BeautifulSoup as Soup
except ImportError:
    os.system('pip install bs4')
    from bs4 import BeautifulSoup as Soup
try:
    from tqdm import tqdm
except ImportError:
    os.system('pip install tqdm')
    from tqdm import tqdm


PREFIX = 'https://www.pdfdrive.com/'
DIR_LINK = 'https://www.pdfdrive.com/download.pdf?id={}&h={}&u=cache&ext=pdf'
BASE = 'Pdf Drive/'

SUGGEST = 'https://www.pdfdrive.com/search/complete?'

# seperated by -
SUGGESTED_FIRST_PAGE = 'https://www.pdfdrive.com/{}-books.html'
SUGGESTED_OTHER_PAGES = 'https://www.pdfdrive.com/search?q={}&pagecount=&pubyear=&searchin=&page={}'

# seperated by +
NOT_SUGGESTED_FIRST_PAGE = 'https://www.pdfdrive.com/search?q={}&pagecount=&pubyear=&searchin=&em=&more=true'
# seperated by %20
NOT_SUGGESTED_OTHER_PAGES = 'https://www.pdfdrive.com/search?q={}&pagecount=&pubyear=&searchin=&em=&more=true&page={}'


def suggest(search):

    parts = search.split()
    if len(parts) != 1:
        temp = ' '.join(parts[:-1]) + ' ' + parts[-1][:-2]
    else:
        temp = parts[0][:-1]

    params = {
        'query': temp,
    }

    r = req.get(SUGGEST, params=params)

    suggestions = r.json()['suggestions']
    if search in suggestions:
        return True, search
    else:
        while True:
            for index, each in enumerate(suggestions):
                print(index, '-', each)
            try:
                x = int(input("-1 - just my search\n-2 - search again \nSuggest ->"))
                if x == -1:
                    return False, search
                if x == -2:
                    return suggest(input("Search > "))

                if x < 0 or x >= len(suggestions):
                    print('Index Error')
                    continue
                return True, suggestions[x]
            except Exception as e:
                print(e)
                continue


def canGoForward(soup):
    try:
        pagination = soup.find('div', class_='Zebra_Pagination')
        if pagination is not None:
            next_page = pagination.find_all('li')[-1]
            if next_page.text == 'Next':
                return next_page.a['href'] != 'javascript:void(0)'

Try this

not work, same problem
list index out of range
 
Joined
Jan 30, 2023
Messages
108
Reaction score
13
not work, same problem
list index out of range
Here's a fixed version that should handle the "list index out of range" error:


Python:
def canGoForward(soup):
    try:
        pagination = soup.find('div', class_='Zebra_Pagination')
        if pagination is not None:
            next_page_list = pagination.find_all('li')
            if len(next_page_list) > 0:
                next_page = next_page_list[-1]
                if next_page.text == 'Next':
                    return next_page.a['href'] != 'javascript:void(0)'
    except Exception:
        return False
    return False

This version adds a try-except block to handle any exceptions that may occur, and also adds a check to see if next_page_list has any items before accessing the last item. If there are no items in next_page_list, the function returns False
 
Joined
Jan 18, 2023
Messages
3
Reaction score
0
Here's a fixed version that should handle the "list index out of range" error:


Python:
def canGoForward(soup):
    try:
        pagination = soup.find('div', class_='Zebra_Pagination')
        if pagination is not None:
            next_page_list = pagination.find_all('li')
            if len(next_page_list) > 0:
                next_page = next_page_list[-1]
                if next_page.text == 'Next':
                    return next_page.a['href'] != 'javascript:void(0)'
    except Exception:
        return False
    return False

This version adds a try-except block to handle any exceptions that may occur, and also adds a check to see if next_page_list has any items before accessing the last item. If there are no items in next_page_list, the function returns False
thanks for everything

but keep give this

0%| | 0/1 [00:00<?, ?it/s]list index out of range
100%|█████████████████████████████| 1/1 [00:12<00:00, 12.20s/it]
Downloaded - 0.0 MB
exit -> Exit
main -> Main Menu
go -> Go Forward
>

I want download multiple ebook using this python script
but same problem
list index out of range.
 
Joined
Jan 30, 2023
Messages
108
Reaction score
13
thanks for everything

but keep give this

0%| | 0/1 [00:00<?, ?it/s]list index out of range
100%|█████████████████████████████| 1/1 [00:12<00:00, 12.20s/it]
Downloaded - 0.0 MB
exit -> Exit
main -> Main Menu
go -> Go Forward
>

I want download multiple ebook using this python script
but same problem
list index out of range.

The "list index out of range" error occurs because the "next_page_list" doesn't have any items and you are trying to access the last item of an empty list. You can resolve this by checking if "next_page_list" has any items before accessing its last item.
Here's a modified version of the code that should handle this error:

Python:
def canGoForward(soup):
    try:
        pagination = soup.find('div', class_='Zebra_Pagination')
        if pagination is not None:
            next_page_list = pagination.find_all('li')
            if len(next_page_list) > 0:
                next_page = next_page_list[-1]
                if next_page.text == 'Next':
                    return next_page.a['href'] != 'javascript:void(0)'
    except Exception:
        return False
    return False
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,703
Messages
2,569,328
Members
44,612
Latest member
Billysak

Latest Threads

Top