This is my code:

from bs4 import BeautifulSoup
import urllib2

url = urllib2.urlopen('http://www.website_address.com')
soup = BeautifulSoup(url)
images = soup.find_all('img')

Now how can I get the "src" of img tags?

Recommended Answers

All 2 Replies

from bs4 import BeautifulSoup
import urllib2

html = '''
<img src="smiley.gif" alt="Smiley face" height="42" width="42">'''

soup = BeautifulSoup(html)
images = soup.find('img')
print(images['src']) #smiley.gif

This seems to be working

>>> url = urllib2.urlopen('http://www.python.org')
>>> soup = BeautifulSoup(url)
>>> srcs = [img['src'] for img in soup.find_all('img')]
>>> srcs
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.