sandy13an
sandy13an

Reputation: 11

Extracting information from HTML file

I'm trying to extract specific information/links from large HTML pages using Python. For example, from the below given HTML output from an IMDb page, I tried extracting the movie links, which look like this:

href="/title/tt2388771/?ref_=nm_flmg_act_1" Jungle Book: Origins

Using the following Python code does not seem to work:

from urllib2 import urlopen
import re

source = urlopen("http://www.imdb.com/name/nm0000288/").read()

print re.findall('href="/title/', source)
print source

Any help/suggestions?

<span class="ghost">|</span> <a href="#self"
onclick="handleFilmoJumpto(this);" data-category="self">Self</a></a>
<span class="ghost">|</span> <a href="#archive_footage"
onclick="handleFilmoJumpto(this);" data-category="archive_footage">Archive footage</a></a>
</div>
<div id="filmography">
<div id="filmo-head-actor" class="head" data-category="actor" onclick="toggleFilmoCategory(this);">
<span id="hide-actor" class="hide-link"
>Hide&nbsp;<img src="http://ia.media-imdb.com/images/G/01/imdb/images/icons/hide-1061525577._CB358668250_.png" class="absmiddle" alt="Hide" width="18" height="16"></span>
<span id="show-actor" class="show-link"
>Show&nbsp;<img src="http://ia.media-imdb.com/images/G/01/imdb/images/icons/show-582987296._CB358668248_.png" class="absmiddle" alt="Show" width="18" height="16"></span>
<a name="actor">Actor</a> (49 credits)
</div>
<div class="filmo-category-section"
>
<div class="filmo-row odd" id="actor-tt2388771">
<span class="year_column">
&nbsp;2017
</span>
<b><a href="/title/tt2388771/?ref_=nm_flmg_act_1"
>Jungle Book: Origins</a></b>
(<a href="/r/legacy-inprod-name/title/tt2388771" class="in_production">filming</a>)
<br/>
<a href="/character/ch0011743/?ref_=nm_flmg_act_1"
>Bagheera</a>
</div>
<div class="filmo-row even" id="actor-tt1596363">
<span class="year_column">
&nbsp;2016
</span>
<b><a href="/title/tt1596363/?ref_=nm_flmg_act_2"
>The Big Short</a></b>
(<a href="/r/legacy-inprod-name/title/tt1596363" class="in_production">filming</a>)
<br/>
Michael Burry
</div>

Upvotes: 1

Views: 141

Answers (1)

lucasnadalutti
lucasnadalutti

Reputation: 5948

No need to search for information in HTML files using regex. Use the worldwide famous Beautiful Soup instead.

Example for your use case:

from urllib2 import urlopen
from bs4 import BeautifulSoup
import re

source = urlopen("http://www.imdb.com/name/nm0000288/").read()

soup = BeautifulSoup(source)
soup.findAll('a', href=re.compile('^/title/'))

Upvotes: 1

Related Questions