मैं सुंदर सूप का उपयोग कर एक वेबसाइट से भाषण को छीनने की कोशिश कर रहा हूं। मुझे समस्याएं आ रही हैं, हालांकि, भाषण को कई अलग-अलग अनुच्छेदों में बांटा गया है। मैं प्रोग्रामिंग के लिए बेहद नया हूं और इस बात से निपटने में परेशानी हो रही है कि इससे कैसे निपटें। पृष्ठ के HTML इस तरह दिखता है:सुंदर सूप और एकाधिक अनुच्छेदों के साथ स्क्रैपिंग
<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.
ऐसा नहीं है कि कुछ समय के लिए की तरह पर जारी है, कई पैराग्राफ टैग के साथ। मैं अवधि के भीतर सभी पाठ निकालने की कोशिश कर रहा हूं।
मैंने टेक्स्ट प्राप्त करने के कुछ अलग-अलग तरीकों की कोशिश की है, लेकिन दोनों जो टेक्स्ट चाहते हैं उसे पाने में विफल रहे हैं।
पहले मैंने कोशिश की है:
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
जो मुझे देता है:
अध्यक्ष, उपाध्यक्ष चेनी, कांग्रेस के सदस्य, प्रतिष्ठित मेहमानों, साथी नागरिकों: हम आज रात को इकट्ठा के रूप में, हमारा राष्ट्र युद्ध में है; हमारी अर्थव्यवस्था मंदी में है; और सभ्य दुनिया को अभूतपूर्व खतरे का सामना करना पड़ता है। फिर भी, हमारे संघ की स्थिति कभी मजबूत नहीं हुई है।
यह पहला अनुच्छेद टैग तक पाठ का हिस्सा है। मैंने फिर कोशिश की:
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()
इसने मुझे पहले अनुच्छेद टैग और दूसरे अनुच्छेद टैग के बीच पाठ दिया। तो, मैं केवल वर्गों की बजाय, संपूर्ण पाठ प्राप्त करने का एक तरीका ढूंढ रहा हूं।
यह प्रश्न में जुड़े वेब पेज के साथ काम नहीं करता है (यानी यह केवल पहले पैराग्राफ को प्रिंट करेगा - पूरे भाषण में नहीं)। – ekhumoro
@ekhumoro निश्चित –
@ShawnChin आपको बहुत बहुत धन्यवाद! यह पूरी तरह से काम किया। – user1074057