Estoy tratando de raspar un discurso de un sitio web usando BeautifulSoup. Sin embargo, estoy teniendo problemas, ya que el discurso está dividido en muchos párrafos diferentes. Soy extremadamente nuevo en la programación y tengo problemas para encontrar la manera de lidiar con esto. El HTML de la página es el siguiente:Raspado con BeautifulSoup y varios párrafos
<span class="displaytext">Thank you very much. Mr. Speaker, Vice President Cheney,
Members of Congress, distinguished guests, fellow citizens: As we gather tonight, our Nation is
at war; our economy is in recession; and the civilized world faces unprecedented dangers.
Yet, the state of our Union has never been stronger.
<p>We last met in an hour of shock and suffering. In 4 short months, our Nation has comforted the victims,
begun to rebuild New York and the Pentagon, rallied a great coalition, captured, arrested, and
rid the world of thousands of terrorists, destroyed Afghanistan's terrorist training camps,
saved a people from starvation, and freed a country from brutal oppression.
<p>The American flag flies again over our Embassy in Kabul. Terrorists who once occupied
Afghanistan now occupy cells at Guantanamo Bay. And terrorist leaders who urged followers to
sacrifice their lives are running for their own.
Se continúa así durante un tiempo, con múltiples etiquetas de párrafo. Estoy tratando de extraer todo el texto dentro del lapso.
He intentado un par de maneras diferentes de obtener el texto, pero ninguno ha podido obtener el texto que quiero.
La primera Probé es:
import urllib2,sys
from BeautifulSoup import BeautifulSoup, NavigableString
address = 'http://www.presidency.ucsb.edu/ws/index.php?pid=29644&st=&st1=#axzz1fD98kGZW'
html = urllib2.urlopen(address).read()
soup = BeautifulSoup(html)
thespan = soup.find('span', attrs={'class': 'displaytext'})
print thespan.string
que me da:
señor presidente, el vicepresidente Cheney, miembros del Congreso, distinguidos invitados, conciudadanos: Al reunirnos esta noche, nuestra Nación está en guerra; nuestra economía está en recesión; y el mundo civilizado enfrenta peligros sin precedentes. Sin embargo, el estado de nuestra Unión nunca ha sido más fuerte.
Esa es la porción del texto hasta la etiqueta del primer párrafo. Luego intenté:
import urllib2,sys
from BeautifulSoup import BeautifulSoup, NavigableString
address = 'http://www.presidency.ucsb.edu/ws/index.php?pid=29644&st=&st1=#axzz1fD98kGZW'
html = urllib2.urlopen(address).read()
soup = BeautifulSoup(html)
thespan = soup.find('span', attrs={'class': 'displaytext'})
for section in thespan:
paragraph = section.findNext('p')
if paragraph and paragraph.string:
print '>', paragraph.string
else:
print '>', section.parent.next.next.strip()
Esto me dio el texto entre la primera etiqueta de párrafo y la segunda etiqueta de párrafo. Entonces, estoy buscando una manera de obtener el texto completo, en lugar de solo secciones.
Esto no funciona con la página web vinculada a en la pregunta (es decir, solo imprimirá el primer párrafo, no el discurso completo). – ekhumoro
@ekhumoro corregido –
@ShawnChin ¡Muchas gracias! Eso funcionó perfectamente. – user1074057