Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 2.7 Beautiful Soup Img Src Extract

Tags:

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

'''

like image 688
phales15 Avatar asked Nov 27 '11 23:11

phales15


People also ask

What is the difference between Find_all () and find () in beautiful soup?

find is used for returning the result when the searched element is found on the page. find_all is used for returning all the matches after scanning the entire document.


1 Answers

There is more simple solution:

 soup.find('img')['src']
like image 139
StanleyD Avatar answered Sep 21 '22 13:09

StanleyD