Reputation: 25
i'm doing "the feeling lucky project" from "automate the boring stuff with python" which i have to open 5 new tabs in the browser based on what you search on google. Using bs4 i managed to get all the links related with search on a list named "links" which looked like this:
and i noticed that before the '#' is the link i actually need, so i tried
for i in links:
if '#' in i:
print(links.index(i))
and i would get index of '#' - 1, so i could get the link. But when i run the code it just prints 1, 1, 1, 1, 1, 1, 1. Which is the index of the first '#' and my loop is stuck there. Can someone explain me why?
Thank you!
Upvotes: 0
Views: 39
Reputation: 191743
Which is the index of the first '#'
It should be the index of the first 'i'
based on your (original} code
This is checking if the link string contains # as a character, which indirectly ensures its in the list, I guess... But likely not what you want
for i in links:
if '#' in i:
If you just want one result, remove the loop and use the correct character
idx = links.index('#')
if idx >= 0:
print(idx, links[idx])
else:
print('not found')
'#' in links
just returns true or false, it won't tell you where it is
If you want all matches, that would be this (though just a list of #, which seems pointless)
[x for x in links if x == '#']
If you want all links before #, you can get the links directly, or you can append in the index as well
res = []
for i in range(1,len(links)): # start one ahead because we're subtracting later
if links[i] == '#':
res.append(links[i-1])
print(res)
Upvotes: 1