[Django]-How to select all "src" in below code for beautiful soup

2👍

Use BeautifulSoup

soup = BeautifulSoup(html)
for i in soup.select('.thumbnails-wrapper img'):
    print i['src']

or

print [i.get('src', None) for i in soup.select('.thumbnails-wrapper img')]

1👍

Try this

import re
p = re.compile(ur'src="([^"]+)')
test_str = u"<div class=\"thumbnails-wrapper\" style=\"display: inline-block; visibility: visible;\">\n<div class=\"thumbnails\" data-expand-target=\"thumbnails\">\n<span class=\"details-trailer\">\n<span class=\"video-image-wrapper\">\n<span class=\"preview-overlay-container\" data-video-url=\"https://www.youtube.com/embed/lXaGPCSqlsM?ps=play&vq=large&rel=0&autohide=1&showinfo=0&autoplay=1&authuser=0\" data-docid=\"com.gameloft.android.ANMP.GloftAGHM\">\n</span>\n<img class=\"screenshot clickable\" tabindex=\"0\" itemprop=\"screenshot\" title=\" Asphalt Nitro- screenshot thumbnail \" alt=\" Asphalt Nitro- screenshot thumbnail \" src=\"//lh3.googleusercontent.com/x7dX056Uo1zlIsTx1rN2NO19g3ETS2J-8A6rmrypc-k_VPdMY5hnoFSVS-MENXfUfqBL=h310\" data-expand-to=\"full-screenshot-0\">\n<img class=\"screenshot clickable\" tabindex=\"0\" itemprop=\"screenshot\" title=\" Asphalt Nitro- screenshot thumbnail \" alt=\" Asphalt Nitro- screenshot thumbnail \" src=\"//lh3.googleusercontent.com/iXwUtiXyragrVn6BeQq2OFFeNR0z1JJVaNELuQscWlizBiwn-dGO1Jb9T8UO29NW2DI=h310\" data-expand-to=\"full-screenshot-1\">\n<img class=\"screenshot clickable\" tabindex=\"0\" itemprop=\"screenshot\" title=\" Asphalt Nitro- screenshot thumbnail \" alt=\" Asphalt Nitro- screenshot thumbnail \" src=\"//lh3.googleusercontent.com/ueH3VEOiN414QwlDUtoRE4-xyV1ucHF3puKRXHtMeJFrkqrlAeC_OcVHV3JKGYiKC8A=h310\" data-expand-to=\"full-screenshot-2\">\n<img class=\"screenshot clickable\" tabindex=\"0\" itemprop=\"screenshot\" title=\" Asphalt Nitro- screenshot thumbnail \" alt=\" Asphalt Nitro- screenshot thumbnail \" src=\"//lh3.googleusercontent.com/2VIXmIvny4xxbDVALMUumX4hdIM38P9OHp0PhT0ktFT7CyNcEX22QjpSsI6hQaWRYxM=h310\" data-expand-to=\"full-screenshot-3\">\n<img class=\"screenshot clickable\" tabindex=\"0\" itemprop=\"screenshot\" title=\" Asphalt Nitro- screenshot thumbnail \" alt=\" Asphalt Nitro- screenshot thumbnail \" src=\"//lh3.googleusercontent.com/fxSqT5CXu1TmSS2MMjmPXnEXbzIMaBr2mlA3SdA1lRI3rr4IMwPUBHAvS4_UE1uXZN73=h310\" data-expand-to=\"full-screenshot-4\">\n<img class=\"screenshot clickable\" tabindex=\"0\" itemprop=\"screenshot\" title=\" Asphalt Nitro- screenshot thumbnail "

m = re.findall(p, test_str)
print m

Output:

[u'//lh3.googleusercontent.com/x7dX056Uo1zlIsTx1rN2NO19g3ETS2J-8A6rmrypc-k_VPdMY5hnoFSVS-MENXfUfqBL=h310', u'//lh3.googleusercontent.com/iXwUtiXyragrVn6BeQq2OFFeNR0z1JJVaNELuQscWlizBiwn-dGO1Jb9T8UO29NW2DI=h310', u'//lh3.googleusercontent.com/ueH3VEOiN414QwlDUtoRE4-xyV1ucHF3puKRXHtMeJFrkqrlAeC_OcVHV3JKGYiKC8A=h310', u'//lh3.googleusercontent.com/2VIXmIvny4xxbDVALMUumX4hdIM38P9OHp0PhT0ktFT7CyNcEX22QjpSsI6hQaWRYxM=h310', u'//lh3.googleusercontent.com/fxSqT5CXu1TmSS2MMjmPXnEXbzIMaBr2mlA3SdA1lRI3rr4IMwPUBHAvS4_UE1uXZN73=h310']

Use BS4

test_str = u"<div class=\"thumbnails-wrapper\" style=\"display: inline-block; visibility: visible;\">\n<div class=\"thumbnails\" data-expand-target=\"thumbnails\">\n<span class=\"details-trailer\">\n<span class=\"video-image-wrapper\">\n<span class=\"preview-overlay-container\" data-video-url=\"https://www.youtube.com/embed/lXaGPCSqlsM?ps=play&vq=large&rel=0&autohide=1&showinfo=0&autoplay=1&authuser=0\" data-docid=\"com.gameloft.android.ANMP.GloftAGHM\">\n</span>\n<img class=\"screenshot clickable\" tabindex=\"0\" itemprop=\"screenshot\" title=\" Asphalt Nitro- screenshot thumbnail \" alt=\" Asphalt Nitro- screenshot thumbnail \" src=\"//lh3.googleusercontent.com/x7dX056Uo1zlIsTx1rN2NO19g3ETS2J-8A6rmrypc-k_VPdMY5hnoFSVS-MENXfUfqBL=h310\" data-expand-to=\"full-screenshot-0\">\n<img class=\"screenshot clickable\" tabindex=\"0\" itemprop=\"screenshot\" title=\" Asphalt Nitro- screenshot thumbnail \" alt=\" Asphalt Nitro- screenshot thumbnail \" src=\"//lh3.googleusercontent.com/iXwUtiXyragrVn6BeQq2OFFeNR0z1JJVaNELuQscWlizBiwn-dGO1Jb9T8UO29NW2DI=h310\" data-expand-to=\"full-screenshot-1\">\n<img class=\"screenshot clickable\" tabindex=\"0\" itemprop=\"screenshot\" title=\" Asphalt Nitro- screenshot thumbnail \" alt=\" Asphalt Nitro- screenshot thumbnail \" src=\"//lh3.googleusercontent.com/ueH3VEOiN414QwlDUtoRE4-xyV1ucHF3puKRXHtMeJFrkqrlAeC_OcVHV3JKGYiKC8A=h310\" data-expand-to=\"full-screenshot-2\">\n<img class=\"screenshot clickable\" tabindex=\"0\" itemprop=\"screenshot\" title=\" Asphalt Nitro- screenshot thumbnail \" alt=\" Asphalt Nitro- screenshot thumbnail \" src=\"//lh3.googleusercontent.com/2VIXmIvny4xxbDVALMUumX4hdIM38P9OHp0PhT0ktFT7CyNcEX22QjpSsI6hQaWRYxM=h310\" data-expand-to=\"full-screenshot-3\">\n<img class=\"screenshot clickable\" tabindex=\"0\" itemprop=\"screenshot\" title=\" Asphalt Nitro- screenshot thumbnail \" alt=\" Asphalt Nitro- screenshot thumbnail \" src=\"//lh3.googleusercontent.com/fxSqT5CXu1TmSS2MMjmPXnEXbzIMaBr2mlA3SdA1lRI3rr4IMwPUBHAvS4_UE1uXZN73=h310\" data-expand-to=\"full-screenshot-4\">\n<img class=\"screenshot clickable\" tabindex=\"0\" itemprop=\"screenshot\" title=\" Asphalt Nitro- screenshot thumbnail "
from bs4 import BeautifulSoup
soup = BeautifulSoup(test_str, 'html.parser')
for link in soup.find_all('img'):
    print(link.get('src'))

Output:

//lh3.googleusercontent.com/x7dX056Uo1zlIsTx1rN2NO19g3ETS2J-8A6rmrypc-k_VPdMY5hnoFSVS-MENXfUfqBL=h310
//lh3.googleusercontent.com/iXwUtiXyragrVn6BeQq2OFFeNR0z1JJVaNELuQscWlizBiwn-dGO1Jb9T8UO29NW2DI=h310
//lh3.googleusercontent.com/ueH3VEOiN414QwlDUtoRE4-xyV1ucHF3puKRXHtMeJFrkqrlAeC_OcVHV3JKGYiKC8A=h310
//lh3.googleusercontent.com/2VIXmIvny4xxbDVALMUumX4hdIM38P9OHp0PhT0ktFT7CyNcEX22QjpSsI6hQaWRYxM=h310
//lh3.googleusercontent.com/fxSqT5CXu1TmSS2MMjmPXnEXbzIMaBr2mlA3SdA1lRI3rr4IMwPUBHAvS4_UE1uXZN73=h310
👤Tim007

Leave a comment