Your identation is off.
I would encurage you to read and follow: How to debug small programs (#2) and familiarize yourself with a debugger - they are great tools to fix your errors yourself.
Python uses indented block to group things together - mostly for loops / conditions / try catch / file operations:
for in in range(10):
if 1==2:
try: ... except:
with open(...) as f:
or f.e for function/classes/etc.
Your print()
command is outside your loop so it will only ever print the last title that was captured in the for
loop above it.
Fix it like this:
for container in containers:
title = container.a.text
print("title: " + title) # this needs to be indented to belong _into_ the loop
Be aware that container
might not contain anything (on other pages) and that the li
-elements also might not contain a "a href
" inside (on other pages) - directly accessing container.a.text
might give you an error if container does not contain any a
- tag.
Use error handling: ask-forgiveness-not-permission-explain to catch errors when they occure to make your code more robust.
See https://docs.python.org/3.6/tutorial/errors.html