9

Can someone help me parse a html file to get the links for all the images in the file in python?

Preferably with out a 3rd party module...

Thanks!

user377419
  • 4,681
  • 13
  • 42
  • 56

3 Answers3

11

You can use Beautiful Soup. I know you said without a 3rd party module. However, this is an ideal tool for parsing HTML.

import urllib2
from BeautifulSoup import BeautifulSoup
page = BeautifulSoup(urllib2.urlopen("http://www.url.com"))
page.findAll('img')
citruspi
  • 6,709
  • 4
  • 27
  • 43
Russell Dias
  • 70,980
  • 5
  • 54
  • 71
11

only using PSL

from html.parser import HTMLParser
class MyParse(HTMLParser):
    def handle_starttag(self, tag, attrs):
        if tag=="img":
            print(dict(attrs)["src"])

h=MyParse()
page=open("index.html").read()
h.feed(page)
Kabie
  • 10,489
  • 1
  • 38
  • 45
2

It's generally accepted that lxml is faster than Beautiful Soup (ref). Its tutorial can be found here: (link) You may also take a look at this old stackoverflow post.

Community
  • 1
  • 1
Overmind Jiang
  • 623
  • 5
  • 17