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?
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?
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...
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.