2011-11-30 9 views
9

मैं सुंदर सूप का उपयोग कर एक वेबसाइट से भाषण को छीनने की कोशिश कर रहा हूं। मुझे समस्याएं आ रही हैं, हालांकि, भाषण को कई अलग-अलग अनुच्छेदों में बांटा गया है। मैं प्रोग्रामिंग के लिए बेहद नया हूं और इस बात से निपटने में परेशानी हो रही है कि इससे कैसे निपटें। पृष्ठ के 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() 

इसने मुझे पहले अनुच्छेद टैग और दूसरे अनुच्छेद टैग के बीच पाठ दिया। तो, मैं केवल वर्गों की बजाय, संपूर्ण पाठ प्राप्त करने का एक तरीका ढूंढ रहा हूं।

उत्तर

8
import urllib2,sys 
from BeautifulSoup import BeautifulSoup 

address = 'http://www.presidency.ucsb.edu/ws/index.php?pid=29644&st=&st1=#axzz1fD98kGZW' 
soup = BeautifulSoup(urllib2.urlopen(address).read()) 

span = soup.find("span", {"class":"displaytext"}) # span.string gives you the first bit 
paras = [x.contents[0] for x in span.findAllNext("p")] # this gives you the rest 
# use .contents[0] instead of .string to deal with last para that's not well formed 

print "%s\n\n%s" % (span.string, "\n\n".join(paras)) 

के रूप में टिप्पणी में कहा, ऊपर इतनी अच्छी तरह से काम नहीं करता है, तो <p> टैग अधिक नेस्टेड टैग शामिल। इस का उपयोग कर के साथ निपटा जा सकता है:

paras = ["".join(x.findAll(text=True)) for x in span.findAllNext("p")] 

हालांकि, कि पिछले <p> कि एक बंद टैग नहीं करता है के साथ बहुत अच्छी तरह काम नहीं करता। एक हैकी कामकाज अलग-अलग इलाज करना होगा। उदाहरण के लिए:

import urllib2,sys 
from BeautifulSoup import BeautifulSoup 

address = 'http://www.presidency.ucsb.edu/ws/index.php?pid=29644&st=&st1=#axzz1fD98kGZW' 
soup = BeautifulSoup(urllib2.urlopen(address).read()) 
span = soup.find("span", {"class":"displaytext"}) 
paras = [x for x in span.findAllNext("p")] 

start = span.string 
middle = "\n\n".join(["".join(x.findAll(text=True)) for x in paras[:-1]]) 
last = paras[-1].contents[0] 
print "%s\n\n%s\n\n%s" % (start, middle, last) 
+0

यह प्रश्न में जुड़े वेब पेज के साथ काम नहीं करता है (यानी यह केवल पहले पैराग्राफ को प्रिंट करेगा - पूरे भाषण में नहीं)। – ekhumoro

+0

@ekhumoro निश्चित –

+0

@ShawnChin आपको बहुत बहुत धन्यवाद! यह पूरी तरह से काम किया। – user1074057

2

यह ऐसे lxml के साथ किया जा जाएगा:

import lxml.html as lh 

tree = lh.parse('http://www.presidency.ucsb.edu/ws/index.php?pid=29644&st=&st1=#axzz1fD98kGZW') 

text = tree.xpath("//span[@class='displaytext']")[0].text_content() 

वैकल्पिक रूप से, इस सवाल का जवाब कैसे BeautifulSoup का उपयोग कर एक ही बात को प्राप्त करने को शामिल किया गया: BeautifulSoup - easy way to to obtain HTML-free contents

स्वीकार किए जाते हैं जवाब से सहायक समारोह:

def textOf(soup): 
    return u''.join(soup.findAll(text=True)) 
+1

शायद सेशन जानते हो क्यों lxml BeautifulSoup के लिए एक अच्छा विकल्प है :) –

+0

जाने न के इन सुझावों उत्पादन प्रश्न में के लिए कहा का उत्पादन करेगा। – ekhumoro

+0

@ekhumoro, क्या आप कृपया समझ सकते हैं कि मेरा समाधान वांछित आउटपुट का उत्पादन करने में विफल रहता है? ओपी चाहता है कि \ "अवधि के भीतर सभी पाठ निकालें", और यही उपर्युक्त कोड है .. – Acorn

0

आप प्रयास करना चाहिए:

0,123,
soup.span.renderContents() 
+0

'.renderContents() 'ओपी चाहता है जो नहीं करता है। यह अनुच्छेद टैग को हटा नहीं देता है। – Acorn

संबंधित मुद्दे