Question

This is a subquestion of this one: Python associate urls's ids and url's titles in lists

I have this HTML script:

<a href="http://pluzz.francetv.fr/videos/monte_le_son_live_,101973832.html"
   class="ss-titre">Monte le son</a>
<div class="rs-cell-details">
    <a href="http://pluzz.francetv.fr/videos/monte_le_son_live_,101973832.html" 
       class="ss-titre">"Rubin_Steiner"</a>
    <a href="http://pluzz.francetv.fr/videos/fare_maohi_,102103928.html"
       class="ss-titre">Fare maohi</a>

How can I do to have this result with BeautifulSoup:

list_titre = [['Monte le son', 'Rubin_Steiner'], ['Fare maohi']]   #one sublist by id

I tried this:

f = urllib.urlopen(url)
page = f.read()
f.close()    
soup = BeautifulSoup(page)
show=[]
list_titre=[]
list_url=[]
for link in soup.findAll('a'):
    lien = link.get('href')
    if lien == None:
       lien = ""
    if "http://pluzz.francetv.fr/videos/" in lien:                 
       titre = (link.text.strip())
    if "Voir cette  vidéo" in titre:
       titre = ""
    if "Lire la vidéo" in titre:
       titre = ""
    list_titre.append(titre) 
    list_url.append(lien)

My result is:

list_titre = ['Monte le son', 'Rubin_Steiner', 'Fare maohi']
list_url = [http://pluzz.francetv.fr/videos/monte_le_son_live_,101973832.html, http://pluzz.francetv.fr/videos/fare_maohi_,102103928.html]

But "titre" is not sorted by id.

Was it helpful?

Solution

Search for your links with a CSS selector to limit hits to just qualifying URLs.

Collect the links in a dictionary by URL; that way you can then process the information by sorting the dictionary keys:

from bs4 import BeautifulSoup

links = {}
soup = BeautifulSoup(page)

for link in soup.select('a[href^=http://pluzz.francetv.fr/videos/]'):
    title = link.get_text().strip()
    if title and title not in (u'Voir cette  vidéo', u'Lire la vidéo'):
        url = link['href']
        links.setdefault(url, []).append(title)

The dict.setdefault() call sets an empty list for urls not yet encountered; this produces a dictionary with the URLs as keys, and the titles as a list of values per URL.

Demo:

>>> page = '''\
... <a href="http://pluzz.francetv.fr/videos/monte_le_son_live_,101973832.html"
...    class="ss-titre">Monte le son</a>
... <div class="rs-cell-details">
...     <a href="http://pluzz.francetv.fr/videos/monte_le_son_live_,101973832.html" 
...        class="ss-titre">"Rubin_Steiner"</a>
...     <a href="http://pluzz.francetv.fr/videos/fare_maohi_,102103928.html"
...        class="ss-titre">Fare maohi</a>
... '''
>>> links = {}
>>> soup = BeautifulSoup(page)
>>> for link in soup.select('a[href^=http://pluzz.francetv.fr/videos/]'):
...     title = link.get_text().strip()
...     if title and title not in (u'Voir cette  vidéo', u'Lire la vidéo'):
...         url = link['href']
...         links.setdefault(url, []).append(title)
... 
>>> from pprint import pprint
>>> pprint(links)
{'http://pluzz.francetv.fr/videos/ce_soir_ou_jamais_,101506826.html': [u'Ce soir (ou jamais !)',
                                                                       u'"Qui est propri\xe9taire de quoi ? La propri\xe9t\xe9 mise \xe0 mal dans tous les domaines"'],
 'http://pluzz.francetv.fr/videos/clip_locaux_,102890631.html': [u'Clips'],
 'http://pluzz.francetv.fr/videos/fare_maohi_,102152859.html': [u'Fare maohi'],
 'http://pluzz.francetv.fr/videos/fare_maohi_,102292937.html': [u'Fare maohi'],
 'http://pluzz.francetv.fr/videos/fare_maohi_,102365651.html': [u'Fare maohi'],
 'http://pluzz.francetv.fr/videos/inspecteur_barnaby_,101972045.html': [u'Inspecteur Barnaby',
                                                                        u'"La musique en h\xe9ritage"'],
 'http://pluzz.francetv.fr/videos/le_lab_o_saison3_,101215383.html': [u'Le Lab.\xd4',
                                                                      u'"Episode 22"',
                                                                      u'Saison 3'],
 'http://pluzz.francetv.fr/videos/monsieur_madame_saison1_,101970319.html': [u'Les Monsieur Madame',
                                                                             u'"Musique"',
                                                                             u'Saison 1'],
 'http://pluzz.francetv.fr/videos/monte_le_son_live_,101973832.html': [u'Monte le son !',
                                                                       u'"Rubin Steiner"'],
 'http://pluzz.francetv.fr/videos/music_explorer_saison1_,101215382.html': [u'Music Explorer : les chasseurs de sons',
                                                                            u'"Episode 3/6"',
                                                                            u'Saison 1'],
 'http://pluzz.francetv.fr/videos/retour_a_goree_,101641108.html': [u'Retour \xe0 Gor\xe9e'],
 'http://pluzz.francetv.fr/videos/singe_mi_singe_moi_,101507102.html': [u'Singe mi singe moi',
                                                                        u'"Le chat"'],
 'http://pluzz.francetv.fr/videos/singe_mi_singe_moi_,101777072.html': [u'Singe mi singe moi',
                                                                        u'"L\'autruche"'],
 'http://pluzz.francetv.fr/videos/toute_nouvelle_tendance_,102472310.html': [u'T.N.T'],
 'http://pluzz.francetv.fr/videos/toute_nouvelle_tendance_,102472336.html': [u'T.N.T'],
 'http://pluzz.francetv.fr/videos/toute_nouvelle_tendance_,102721018.html': [u'T.N.T'],
 'http://pluzz.francetv.fr/videos/toute_nouvelle_tendance_,103216774.html': [u'T.N.T.'],
 'http://pluzz.francetv.fr/videos/toute_nouvelle_tendance_,103216788.html': [u'T.N.T'],
 'http://pluzz.francetv.fr/videos/via_cultura_,101959892.html': [u'Via cultura',
                                                                 u'"L\'Ochju, le Mauvais oeil"']}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top