Python: Get Image Link From Html
Solution 1:
lxml
is the tool for the job.
To scrape all the images from a webpage would be as simple as this:
import lxml.html
tree = lxml.html.parse("http://example.com")
images = tree.xpath("//img/@src")
print images
Giving:
['/_img/iana-logo-pageheader.png', '/_img/icann-logo-micro.png']
If it was an RSS feed, you'd want to parse it with lxml.etree
.
Solution 2:
Using urllib and beautifulsoup:
import urllib
from BeautifulSoup import BeautifulSoup
f = urllib.urlopen(url)
page = f.read()
f.close()
soup = BeautifulSoup(page)
for link in soup.findAll('img'):
print"IMAGE LINKS:", link.get('data-src')
Solution 3:
Perhaps you should start with reading Regex Howto tutorial and a FAQ in the StackOverflow which says that whenever you are dealing with XMLs (HTML) dont use Regex, but rather using good parsers and in your case, BeautifulSoup is one.
Using Regex, you would do this to get the link to your image:
import re
pattern = re.compile(r'src="(http://.*\.jpg)"')
pattern.search("yourhtmlcontainingtheimagelink").group(1)
Solution 4:
To add to svick's answer, try using the BeautifuSoup parser, it worked for me in the past.
Solution 5:
get html tag data, according to tornado spider
from HTMLParser import HTMLParser
defget_links(html):
classURLSeeker(HTMLParser):
def__init__(self):
HTMLParser.__init__(self)
self.urls = []
defhandle_starttag(self, tag, attrs):
if tag == 'img':
src = dict(attrs).get('src')
if src:
self.urls.append(src)
url_seeker = URLSeeker()
url_seeker.feed(html)
return url_seeker.urls
Post a Comment for "Python: Get Image Link From Html"