You can use BeautifulSoup to extract src attribute of an html img tag. In my example, the htmlText contains the img tag itself but this can be used for a URL too along with urllib2.
The solution provided by the most rated answer is not working any more with python3. This is the correct implementation:
For URLs
from bs4 import BeautifulSoup as BSHTML
import urllib3
http = urllib3.PoolManager()
url = 'your_url'
response = http.request('GET', url)
soup = BSHTML(response.data, "html.parser")
images = soup.findAll('img')
for image in images:
#print image source
print(image['src'])
#print alternate text
print(image['alt'])
For Texts with img tag
from bs4 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'])
a.attrs['src']
to work? There's no<a>
tag with asrc
attribute in the snippet you've shown. - jwoddersrc
.what's the other questions ? - iDelusion