Mi piacerebbe avere tutto il testo visibile da un sito Web, dopo il rendering dell'HTML. Sto lavorando in Python con Scrapy framework. Con xpath('//body//text()')
Sono in grado di ottenerlo, ma con i tag HTML, e voglio solo il testo. Qualche soluzione per questo? Grazie !Come posso ottenere tutto il testo normale da un sito Web con Scrapy?
risposta
L'opzione più semplice sarebbe quella di extract
//body//text()
e join
tutto trovato:
''.join(sel.select("//body//text()").extract()).strip()
dove sel
è un'istanza Selector
.
Un'altra opzione è quella di utilizzare nltk
's clean_html()
:
>>> import nltk
>>> html = """
... <div class="post-text" itemprop="description">
...
... <p>I would like to have all the text visible from a website, after the HTML is rendered. I'm working in Python with Scrapy framework.
... With <code>xpath('//body//text()')</code> I'm able to get it, but with the HTML tags, and I only want the text. Any solution for this? Thanks !</p>
...
... </div>"""
>>> nltk.clean_html(html)
"I would like to have all the text visible from a website, after the HTML is rendered. I'm working in Python with Scrapy framework.\nWith xpath('//body//text()') I'm able to get it, but with the HTML tags, and I only want the text. Any solution for this? Thanks !"
Un'altra opzione è quella di utilizzare BeautifulSoup
' s get_text()
:
get_text()
If you only want the text part of a document or tag, you can use the
get_text()
method. It returns all the text in a document or beneath a tag, as a single Unicode string.
>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup(html)
>>> print soup.get_text().strip()
I would like to have all the text visible from a website, after the HTML is rendered. I'm working in Python with Scrapy framework.
With xpath('//body//text()') I'm able to get it, but with the HTML tags, and I only want the text. Any solution for this? Thanks !
Un'altra opzione è quella di utilizzare lxml.html
s' text_content()
:
.text_content()
Returns the text content of the element, including the text content of its children, with no markup.
>>> import lxml.html
>>> tree = lxml.html.fromstring(html)
>>> print tree.text_content().strip()
I would like to have all the text visible from a website, after the HTML is rendered. I'm working in Python with Scrapy framework.
With xpath('//body//text()') I'm able to get it, but with the HTML tags, and I only want the text. Any solution for this? Thanks !
Hai provato?
xpath('//body//text()').re('(\w+)')
O
xpath('//body//text()').extract()
In realtà funziona piuttosto bene, ma restituisce ancora alcuni tag HTML e altri. – tomasyany
- 1. Scrapy - Scansiona tutto il sito web
- 2. scrapy - come ottenere il testo da 'div'
- 3. Scrap sito web utilizzando Scrapy
- 4. Posizionare assolutamente tutto su un sito web?
- 5. Come posso riprodurre un file PLS da un sito Web?
- 6. Scrapy dati Python con Scrapy
- 7. Come ottenere favicon.ico da un sito Web utilizzando Java?
- 8. Come ottenere innerHTML di un nodo utilizzando il Selettore scrapy?
- 9. PowerShell + WebAdministration - Come ottenere il sito Web dall'applicazione web?
- 10. come posso testare il mio sito Web con ie6
- 11. Esiste un modo per consentire agli utenti di copiare il testo da un sito Web e il testo non formattato?
- 12. Come ottenere il valore del cookie nel sito Web asp.net
- 13. Come posso accedere a un sito Web con Python?
- 14. ottenere miniature da un sito Web dai propri url.
- 15. Come inserire testo normale?
- 16. Associare un URL da un sito Web a un altro controller Sito web con IIS7.5
- 17. Come posso raccogliere dati da un sito Web che utilizza AJAX, con Perl?
- 18. Utilizzo di Scrapy per trovare e scaricare file PDF da un sito Web
- 19. ottenere vietato dalla robots.txt: Scrapy
- 20. Come posso effettuare ricerche su tutto il Web da Ricerca personalizzata Google?
- 21. Posso ottenere il testo evidenziato con JQuery?
- 22. Come posso ottenere la pagina padre da un controllo utente in un sito Web ASP.NET (non un'applicazione Web)
- 23. Come posso creare un thread che viene eseguito tutto il tempo in background in un sito Web .net?
- 24. Come posso utilizzare FTP per aggiornare il mio sito Web da un repository Subversion?
- 25. Come posso generare un sito con sito mvn: sito?
- 26. Ottenere il testo da un nodo
- 27. Come posso ottenere il testo da UITextField in una NSString?
- 28. Come si scrive il controllo di un sito Web?
- 29. Tutto su come scegliere il font giusto per un sito web
- 30. Come ottenere il valore normale da azioni IO in Haskell
Ho eliminato la mia domanda .. Ho usato il seguente codice html = sel.select ("// body // text()") tree = lxml.html.fromstring (html) elemento ['description'] = tree.text_content(). strip() Ma sto ricevendo il \t is_full_html = _looks_like_full_html_unicode (html) \t exceptions.TypeError: stringa attesa o buffer ..erro. Cosa è andato storto – Backtrack
'nltk' ha funzionato meglio per me – user4421975
Proprio come un aggiornamento,' nltk' deprecato loro 'metodo clean_html' invece consiglia: ' NotImplementedError: Per rimuovere markup HTML, utilizzare get_text di BeautifulSoup() Funzione ' – TheNastyOne