Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
209 views
in Technique[技术] by (71.8m points)

python - How to Loop a List and Extract required data (Beautiful Soup)

I need help in looping a list and extracting the src links. This is my list and the code:

getimages = getDetails.find_all('img')
#deleting the first image in the list
getimages[0].decompose()
print(getimages)

The output of getimages list is :

[<None></None>]
[<None></None>, <img border="0" data-original-height="855" data-original-width="1885" src="https://1.bp.blogspot.com/-mq2ilVOcyPQ/X70khVD9UaI/AAAAAAAArLw/xC2LggPdcRUTm3aTGpPFYhoM6rDJwbyzACLcBGAsYHQ/s16000-rw/ssc-admit-card.webp"/>]
[<None></None>]

This is how I am looping to extract the src image :

try:
  for x in getimages:
      print (x['src'])
except :
    print("Image not found")

The output is always Image Not found , but the image is present in the list, How can i fix it, please guide. Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Solution: use regEx

import re
regular_expression = r'<img.*?src="(.*?)".*?>'
for ia in getimages:
    list_link = list(re.findall(regular_expression, ia))
    print(list_link[-1])

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...