20
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

'''

0

4 Answers 4

43

There is more simple solution:

 soup.find('img')['src']
1
  • 2
    5 years later and this is still the most elegant.
    – remykarem
    Jul 2, 2018 at 18:14
31

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'.

0

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)
1
  • Still getting the error: Traceback (most recent call last): File "C:\Users\BuyzDirect\Desktop\OverStock_Listing_Format_Tool.py", line 50, in <module> findPatImgSrc = patImgSrc.findall(imgsrc) TypeError: expected string or buffer
    – phales15
    Nov 27, 2011 at 23:49
0

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']

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.