How to get the title of links in the html page using BeautifulSoup python?
<p>Consider the html :</p>

<pre><code>&lt;li&gt;&lt;img alt="SangamUniversity-animated" class="newimg" src="images/new_animated.gif" /&gt;&lt;a href="pdffiles/Special Exam_Aug_2016.pdf" target="_blank"&gt; Student Notice &lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;img alt="SangamUniversity-animated" class="newimg" src="images/new_animated.gif" /&gt;&lt;a href="pdffiles/Sangam_University_Bus_Route_Chart_Aug16.pdf" style="font-size:14px" target="_blank"&gt;UPDATED BUS ROUTE &lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;img alt="SangamUniversity-animated" class="newimg" src="images/new_animated.gif" /&gt;&lt;a href="pdffiles/Sangam_University_Faculty_Requirement_Aug2016.jpg" target="_blank"&gt;Application are invited &lt;/a&gt;&lt;/li&gt;
</code></pre>

<p>I want to extract the following titles and save them in a <code>list</code>:</p>

<pre><code>Student Notice
UPDATED BUS ROUTE
Application are invited
</code></pre>

<p>How can I do it using <code>urllib2</code> and <code>BeautifulSoup</code>?</p>

<p>You don't need <code>urllib</code> if you already have the html...urllib is used to make a request to a web server which then returns the html, you can simply do this when you have the html</p>

<pre><code>&gt;&gt;&gt; from bs4 import BeautifulSoup
&gt;&gt;&gt; a = """&lt;li&gt;&lt;img alt="SangamUniversity-animated" class="newimg" src="images/new_animated.gif" /&gt;&lt;a href="pdffiles/Special Exam_Aug_2016.pdf" target="_blank"&gt; Student Notice &lt;/a&gt;&lt;/li&gt;
... &lt;li&gt;&lt;img alt="SangamUniversity-animated" class="newimg" src="images/new_animated.gif" /&gt;&lt;a href="pdffiles/Sangam_University_Bus_Route_Chart_Aug16.pdf" style="font-size:14px" target="_blank"&gt;UPDATED BUS ROUTE &lt;/a&gt;&lt;/li&gt;
... &lt;li&gt;&lt;img alt="SangamUniversity-animated" class="newimg" src="images/new_animated.gif" /&gt;&lt;a href="pdffiles/Sangam_University_Faculty_Requirement_Aug2016.jpg" target="_blank"&gt;Application are invited &lt;/a&gt;&lt;/li&gt;"""
&gt;&gt;&gt; b = BeautifulSoup(a, 'html.parser')
&gt;&gt;&gt; c = b.find_all('li')

&gt;&gt;&gt; for elem in c:
...     print(elem.a.string)
... 
 Student Notice 
UPDATED BUS ROUTE 
Application are invited 
</code></pre>

python web-scraping beautifulsoup urllib2