info-farmer
info-farmer

Reputation: 265

How to complete the code to get the title only from a wikipedia page?

The following code displays some output. From it, how to get the 'title' only?

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from bs4 import BeautifulSoup
import requests

url = u'https://ta.wikisource.org/wiki/அட்டவணை:பாண்டிய நாட்டுக் கோவில்கள்.pdf'
content = requests.get(url).content
soup = BeautifulSoup(content,'lxml')

talkPage1 = soup.findAll(id='ca-talk')
talkPageType = type(talkPage1)
print(talkPage1)

Its output:-

[li class="new" id="ca-talk"><span><a accesskey="t" href="/w/index.php?title=%E0%AE%85%E0%AE%9F%E0%AF%8D%E0%AE%9F%E0%AE%B5%E0%AE%A3%E0%AF%88_%E0%AE%AA%E0%AF%87%E0%AE%9A%E0%AF%8D%E0%AE%9A%E0%AF%81:%E0%AE%AA%E0%AE%BE%E0%AE%A3%E0%AF%8D%E0%AE%9F%E0%AE%BF%E0%AE%AF_%E0%AE%A8%E0%AE%BE%E0%AE%9F%E0%AF%8D%E0%AE%9F%E0%AF%81%E0%AE%95%E0%AF%8D_%E0%AE%95%E0%AF%8B%E0%AE%B5%E0%AE%BF%E0%AE%B2%E0%AF%8D%E0%AE%95%E0%AE%B3%E0%AF%8D.pdf&amp;action=edit&amp;redlink=1" rel="discussion" title="உள்ளடக்கப் பக்கம் தொடர்பான உரையாடல் பக்கம் (இன்னமும் எழுதப்படவில்லை) [t]">உரையாடல்</a></span></li>]

Our need : title="உள்ளடக்கப் பக்கம் தொடர்பான உரையாடல் பக்கம் (இன்னமும் எழுதப்படவில்லை)

Upvotes: 0

Views: 229

Answers (1)

Gahan
Gahan

Reputation: 4213

# coding=utf-8
from bs4 import BeautifulSoup
import requests

url = u'https://ta.wikisource.org/wiki/அட்டவணை:பாண்டிய நாட்டுக் கோவில்கள்.pdf'
content = requests.get(url).content
soup = BeautifulSoup(content,'html.parser')  # use html.parser to parse html

talkPage1 = soup.findAll(id='ca-talk')  # if there is not more than one title to find or to only get first tag with id=ca-talk among all then use find() instead of findAll()
talkPageType = type(talkPage1)
for element in talkPage1:  # findAll() is resultset hence need to iterate to process element
    print(element.find('a')['title']) 

Output:

'உள்ளடக்கப் பக்கம் தொடர்பான உரையாடல் பக்கம் (இன்னமும் எழுதப்படவில்லை) [t]'

In webpage tag contains attribute title is printed as output and [t] is in the title if you don't want it then you can just use .replace('[t]', '') or slice it.

Upvotes: 2

Related Questions