Anymee
Anymee

Reputation: 121

ValueError: too many values to unpack (expected 2) in my code

In this line url, endpos = get_next_target(page), it says ValueError: too many values to unpack (expected 2) in the terminal. I tried to split it but then realized the function get_next_target outputs a list so that wouldn't work.

def get_all_links(page): 
    links = [] 
    while True: 
        url, endpos = get_next_target(page) 
    if url: 
        links.append(url) 
        page = page[endpos:] 
    else: 
        break 
    return links 

Upvotes: 0

Views: 986

Answers (2)

Huy Vo
Huy Vo

Reputation: 2500

If get_next_target(page) returns a list then you can try this:

url, *endpos = get_next_target(page)

Or:

*url, endpos = get_next_target(page)

That way *endpos or *url will become a list. For example if get_next_target(page) returns [a, b, c, d] then url will be a and endpos will be [b, c, d].

Upvotes: 2

zenlc2000
zenlc2000

Reputation: 451

You said it: get_next_target(page) is returning a list and it is trying to stick that list into url and endpos - 2 variables. Since it tells you "too many" that means the list has more than 2 elements.

Upvotes: 1

Related Questions