3

Suppose, that I got URLs of images on the page,

 for i in wiki.images:
    print (i)

Is there easy way to get images titles?

petezurich
  • 9,280
  • 9
  • 43
  • 57
wos
  • 41
  • 3

2 Answers2

3

try:

If you are looping through all the urls of the images then you can try

for i in wiki.images:
    i.split('/')[-1]  # -1 because the name is at the last part of the url

So the above code will give you the image name.

Hope this helps...

Chetan Vashisth
  • 438
  • 4
  • 14
2

If what you are trying to get are the image tag's title attribute (i.e., from the HTML), you could do something akin to:

import wikipedia
from html.parser import HTMLParser

class WikipediaImageParser(HTMLParser):
    def handle_starttag(self, tag, attrs):
        if tag == 'img':
            try:
                print(dict(attrs)['title'])
            except KeyError as e:
                return # do nothing

page = wikipedia.page("History_of_Japan")
parser = WikipediaImageParser()
parser.feed(page.html())

You can parse the HTML to get a dict of the attributes for each image and then just check if there is a title attribute.

David
  • 134
  • 1
  • 10