سلام
عندي مشكلة بال BeautifulSoap and Recursion
الحين استخدمت BeautifulSoap to parse المواقع وبعدين استخدمت Recursion عشان امشي على كل ال tree واستخرج فقط ال text
المشكلة عندي بال Recursion لانه المتغير (Text) تتغير قيمته كل مره أنادي فيها الداله. كيف احل المشكله هذي واحتفظ بالكلمات في نتغير واحد وارجعه للبرنامج؟
شكرا
import re
import urllib2
from BeautifulSoup import BeautifulSoup, NavigableString
html = ['<html><head><title>Page title</title></head>',
'<body><p id="firstpara" align="center">This is paragraph <b>one</b>.',
'<p id="secondpara" align="blah">This is paragraph <b>two</b>.',
'</html>']
def ParseContent(current):
Text =""
if hasattr(current,'contents'):
for next in current.contents:
ParseContent(next)
if isinstance(current, NavigableString):
print str(current)
Text += str(current)
return Text
soup = BeautifulSoup(''.join(html))
Page_Text = ParseContent(soup)
print "Text after function call: ", Page_Text