phales15
phales15

Reputation: 465

Python 2.7 Beautiful Soup Img Src Extract

for imgsrc in Soup.findAll('img', {'class': 'sizedProdImage'}):
    if imgsrc:
        imgsrc = imgsrc
    else:
        imgsrc = "ERROR"

patImgSrc = re.compile('src="(.*)".*/>')
findPatImgSrc = re.findall(patImgSrc, imgsrc)

print findPatImgSrc

'''
<img height="72" name="proimg" id="image" class="sizedProdImage" src="http://imagelocation" />

This is what I am trying to extract from and I am getting:

findimgsrcPat = re.findall(imgsrcPat, imgsrc)
File "C:\Python27\lib\re.py", line 177, in findall
    return _compile(pattern, flags).findall(string)
TypeError: expected string or buffer

'''

Upvotes: 20

Views: 37424

Answers (4)

Abu Shoeb
Abu Shoeb

Reputation: 5152

In my example, the htmlText contains the img tag but it can be used for a URL too. See my answer here

from BeautifulSoup import BeautifulSoup as BSHTML
htmlText = """<img src="https://src1.com/" <img src="https://src2.com/" /> """
soup = BSHTML(htmlText)
images = soup.findAll('img')
for image in images:
    print image['src']

Upvotes: 0

StanleyD
StanleyD

Reputation: 2368

There is more simple solution:

 soup.find('img')['src']

Upvotes: 43

soulcheck
soulcheck

Reputation: 36767

You're passing beautifulsoup node to re.findall. You have to convert it to string. Try:

findPatImgSrc = re.findall(patImgSrc, str(imgsrc))

Better yet, use the tools beautifulsoup provides:

[x['src'] for x in soup.findAll('img', {'class': 'sizedProdImage'})]

gives you a list of all src attributes of img tags of class 'sizedProdImage'.

Upvotes: 31

Kirk Strauser
Kirk Strauser

Reputation: 30947

You're creating an re object, then passing it into re.findall which expects a string as the first argument:

patImgSrc = re.compile('src="(.*)".*/>')
findPatImgSrc = re.findall(patImgSrc, imgsrc)

Instead, use the .findall method of the patImgSrc object you just created:

patImgSrc = re.compile('src="(.*)".*/>')
findPatImgSrc = patImgSrc.findall(imgsrc)

Upvotes: 0

Related Questions