2011-03-22 57 views
7

Estoy tratando de raspar el texto solo del cuerpo usando Python Scrapy, pero todavía no he tenido suerte.Texto cuerpo de Scrapy solamente

Deseando que algunos estudiosos puedan ayudarme aquí raspando todo el texto de la etiqueta <body>.

Respuesta

4

Scrapy utiliza la notación XPath para extraer partes de un documento HTML. Entonces, ¿ha intentado simplemente usar la ruta /html/body para extraer <body>? (suponiendo que está anidado en <html>). Puede ser que sea aún más fácil de utilizar el selector de //body:

x.select("//body").extract() # extract body 

Puede encontrar más información sobre los selectores Scrapy proporciona here.

+0

Gracias Eli, sé esa parte. Pero mi pregunta estaba relacionada para obtener texto sin formato en lugar de html. ¿Hay alguna manera en la terapia que sabes? – mmrs151

+0

@ mmrs151: intente agregar '/ text()' al selector. –

+1

adding/text() obtendrá el texto del cuerpo, utilizando // text() obtendrá el texto de todos los subelementos del cuerpo. Pero algunos de esos elementos contendrán etiquetas indeseables como script. – spazm

2

Sería bueno obtener resultados como el producido por lynx -nolist -dump, que muestra la página y luego vuelca el texto visible. Me he acercado extrayendo el texto de todos los elementos secundarios de los párrafos.

Empecé con //body//text(), que extraía todos los elementos de texto dentro del cuerpo, pero esto incluía elementos de guión. //body//p obtiene todos los elementos de párrafo dentro del cuerpo, incluida la etiqueta de párrafo implícita alrededor del texto sin etiquetar. Al extraer el texto con //body//p/text() se pierden elementos de subetiquetas (como negrita, cursiva, span, div). //body//p//text() parece obtener la mayor parte del contenido deseado, siempre y cuando la página no tenga etiquetas de script incrustadas en los párrafos.

en XPath / implica un hijo directo, mientras que // incluye todos los descendientes.

% scrapy shell 
In[1]: fetch('http://stackoverflow.com/questions/5390133/scrapy-body-text-only') 
In[2]: hxs.select('//body//p//text()').extract() 

Out[2]: 
[u"I am trying to scrape the text only from body using python Scrapy, but haven't had any luck yet.", 
u'Wishing some scholars might be able to help me here scraping all the text from the ', 
u'&lt;body&gt;', 
u' tag.', 
u'Thank you in advance for your time.', 
u'Scrapy uses XPath notation to extract parts of a HTML document. So, have you tried just using the ', 
u'/html/body', 
u' path to extract ', 
u'&lt;body&gt;', 
u"? (assuming it's nested in ", 
u'&lt;html&gt;', 
u'). It might be even simpler to use the ', 
u'//body', 
u' selector:', 
u'You can find more information about the selectors Scrapy provides ', 
u'here', 

Unir las cadenas juntas con un espacio y tiene una muy buena salida:

In [43]: ' '.join(hxs.select("//body//p//text()").extract()) 
Out[43]: u"I am trying to scrape the text only from body using python Scrapy, but haven't had any luck yet. Wishing some scholars might be able to help me here scraping all the text from the &lt;body&gt; tag. Thank you in advance for your time. Scrapy uses XPath notation to extract parts of a HTML document. So, have you tried just using the /html/body path to extract &lt;body&gt; ? (assuming it's nested in &lt;html&gt;). It might be even simpler to use the //body selector: You can find more information about the selectors Scrapy provides here . This is a collaboratively edited question and answer site for professional and enthusiast programmers . It's 100% free, no registration required. about \xbb \xa0\xa0\xa0 faq \xbb \r\n    tagged asked 1 year ago viewed 280 times active 1 year ago" 
Cuestiones relacionadas