hexated
hexated

Reputation: 19

python requests module iteration

I was doing a coding challenge and noticed something weird with how arrays worked with modules (or more likely it's my own error).

pages = [*list of 5 webpages*]
final = []

for i in range(0,4):
    page = requests.get(pages[i])
    piece = page.text
    final.append(piece)

print(''.join(final))

This code only joins 4 of the 5 webpages. Changing the range to 0,5 or len(pages) solves the problem. I was under the impression that 0,4 would include all the webpages in my list (5 of them) since indexing starts at 0.

Upvotes: 0

Views: 75

Answers (1)

DYZ
DYZ

Reputation: 57105

The following code is pythonic and more robust in a sense that it does not depend on the number of pages:

pages = [*list of 5 webpages*]
final = [requests.get(page).text for page in pages]
print(''.join(final))

Upvotes: 1

Related Questions