|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Senior Member
Iscritto dal: Aug 2007
Messaggi: 1270
|
[Python] Parsing di un file HTML (risolto)
Sto studiando come accedere a informazioni mirate contenute in una pagina web tramite Python, quindi con PyXML e DOM.
Questo è un esempio che sto studiando, deve aprire una pagina, listare tutte le immagini e stamparne l'attributo "src", però si comporta in modo strano: Codice:
from xml.dom.ext.reader import HtmlLib
reader = HtmlLib.Reader()
doc_node = reader.fromUri("http://www.google.com/firefox")
images = doc_node.documentElement.getElementsByTagName("img")
for image in images:
print image.getAttribute("src")
Se pero effettuo questa chiamata: image.attributes.item(0).nodeValue, ritorna l'attributo src corretto... Mi chiedo perchè getAttribute("src") non mi ritorni pure lui il valore corretto, visto che serve apposta per quello. Ultima modifica di arara : 18-09-2007 alle 23:11. |
|
|
|
|
|
#2 |
|
Senior Member
Iscritto dal: Aug 2007
Messaggi: 1270
|
Se eseguo dir(image) mi stampa questo, dovrebbe essere un dizionario di tuple e oggetti di tipo Node... è qui che il metodo getAttribute("tag") dovrebbe andare a cercare l'attributo.
(l'indentazione l'ho aggiunta io...) Codice:
<NamedNodeMap at 8433b2c:
{
(None, u'src'): <Attribute Node at 8433b8c: Name="src", Value="/images/firefox/clear.gif">,
(None, u'width'): <Attribute Node at 843742c: Name="width", Value="1">,
(None, u'height'): <Attribute Node at 8433fcc: Name="height", Value="20">
}
>
|
|
|
|
|
|
#3 | |
|
Senior Member
Iscritto dal: Dec 2005
Città: Istanbul
Messaggi: 1817
|
Quote:
Una fatta molto bene e' BeautifulSoup: http://www.crummy.com/software/BeautifulSoup/ Codice:
from BeautifulSoup import BeautifulSoup
from urllib2 import urlopen
soup = BeautifulSoup( urlopen('http://www.google.com/firefox').read() )
for image in soup.findAll('img'):
print image['src']
Se poi devi pure navigarci sulle pagine (autenticarti, cliccare un paio di link, scaricare un altro file...) usa anche http://wwwsearch.sourceforge.net/mechanize/
__________________
One of the conclusions that we reached was that the "object" need not be a primitive notion in a programming language; one can build objects and their behaviour from little more than assignable value cells and good old lambda expressions. —Guy Steele |
|
|
|
|
|
|
#4 |
|
Senior Member
Iscritto dal: Aug 2007
Messaggi: 1270
|
bene bene, funziona!
Questo metodo mi sembra perfetto per quello che devo fare: titleTag = soup.html.head.title print titleTag |
|
|
|
|
|
#5 | |
|
Senior Member
Iscritto dal: Jul 2002
Città: Reggio Calabria -> London
Messaggi: 12112
|
Quote:
io lo sto usando in ruby x ora P.S. potevi rispondere anke al mio thread allora quando cercavo qualcosa ke facesse ciò prima di scoprire mechanize
__________________
|
|
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 17:06.




















