I have some troubles with getting the data from the website. The website source is here:
view-source:http://release24.pl/wpis/23714/%22La+mer+a+boire%22+%282011%29+FRENCH.DVDRip.XviD-AYMO
there’s sth like this:
INFORMACJE O FILMIE
Tytuł……………………………………..: La mer à boire
Ocena………………………………………: IMDB – 6.3/10 (24)
Produkcja…………………………………..: Francja
Gatunek…………………………………….: Dramat
Czas
trwania………………………………..:
98 min.Premiera……………………………………: 22.02.2012 – Świat
Reżyseria………………………………….: Jacques Maillot
Scenariusz………………………………….: Pierre Chosson, Jacques Maillot
Aktorzy…………………………………….: Daniel Auteuil, Maud Wyler, Yann Trégouët,
Alain Beigel
And I want to get the data from this website to have a Python list of strings:
[[Tytuł, "La mer à boire"]
[Ocena, "IMDB - 6.3/10 (24)"]
[Produkcja, Francja]
[Gatunek, Dramat]
[Czas trwania, 98 min.]
[Premiera, "22.02.2012 - Świat"]
[Reżyseria, "Jacques Maillot"]
[Scenariusz, "Pierre Chosson, Jacques Maillot"]
[Aktorzy, "Daniel Auteuil, Maud Wyler, Yann Trégouët, Alain Beigel"]]
I wrote some code using BeautifulSoup but I cant go any further, I just don’t know what to get the rest from the website source and how to convert is to string …
Please, help!
My code:
# -*- coding: utf-8 -*-
#!/usr/bin/env python
import urllib2
from bs4 import BeautifulSoup
try :
web_page = urllib2.urlopen("http://release24.pl/wpis/23714/%22La+mer+a+boire%22+%282011%29+FRENCH.DVDRip.XviD-AYMO").read()
soup = BeautifulSoup(web_page)
c = soup.find('span', {'class':'vi'}).contents
print(c)
except urllib2.HTTPError :
print("HTTPERROR!")
except urllib2.URLError :
print("URLERROR!")
The secret of using BeautifulSoup is to find the hidden patterns of your HTML document. For example, your loop
is in the right direction, but it will return all paragraphs, not only the ones you are looking for. The paragraphs you are looking for, however, have the helpful property of having a class
i. Inside these paragraphs one can find two spans, one with the classiand another with the classvi. We are lucky because those spans contains the data you are looking for:So, first get all the paragraphs with the given class:
Now, using list comprehensions, we can generate a list of pairs, where each pair contains the first and the second span from the paragraph:
Now that we have the spans, we can get the texts from them:
Those texts are not ok still, but it is easy to correct them. To remove the dots from the first one, we can use
rstrip():The
:string can be removed withlstrip():To apply it to all content, we just need another list comprehension:
And that is it. I hope this step-by-step example can make the use of BeautifulSoup clearer for you.