0

I'm working on a script to download the images from mangareader, and I've been trying to do it with Python since this is the first language I've learn throughly and I'm really into at the moment. The problem is that the output from the loop I've made is not working the way I was expecting to be.

soup = soup.findAll('option') # We select all the option tags
for l in soup: # And we start with a loop
    l = l.get_text()
    print l
    url = url + str(l)
    print url

The output I was expecting was:

1
http://www.mangareader.net/witch-shop/1/1
2
http://www.mangareader.net/witch-shop/1/2
3
http://www.mangareader.net/witch-shop/1/3
4
http://www.mangareader.net/witch-shop/1/4
5
http://www.mangareader.net/witch-shop/1/5
[...]

And what it shows is:

1
http://www.mangareader.net/witch-shop/1/1
2
http://www.mangareader.net/witch-shop/1/12
3
http://www.mangareader.net/witch-shop/1/123
4
http://www.mangareader.net/witch-shop/1/1234
5
http://www.mangareader.net/witch-shop/1/12345
[...]

And I can't figure out the why.

I appreciate your help.

0

3 Answers 3

3

Well, on each iteration you do

url = url + str(l)

which appends digits to url, and it's never reset.

To get desired output, try

for l in soup:
    l = l.get_text()
    print l
    print url + str(l)

This way you don't change the contents of the url variable.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you as well for your time and help!
2

It's because you are appending l to url on every iteration without initializing url.

You should do:

#....
url2 = url + str(l)
print url2
#....

Comments

1

When you are doing the following

 url = url + str(l)

You are actually appending to the string, not adding 1 to it.

2 Comments

@LeandroPoblet Welcome to stackoverflow. Its great to know that we helped but you really don't need to thank everyone :P
@LevLevitsky All I'm saying is it might get a bit ... difficult at some points. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.