需要使用BeautifulSoup提取标记中的所有字符或将完整标记转换为字符串

问题描述:

我需要将Tag对象或'node'转换为字符串。 这里是我的代码:需要使用BeautifulSoup提取标记中的所有字符或将完整标记转换为字符串

import urllib 
from bs4 import BeautifulSoup 
class scraping: 
    site = urllib.urlopen("http://www.bbc.com/news/world-us-canada-36466228")   
    myfile = site.read() 
    soup = BeautifulSoup(myfile) 
    text = "" 
    for node in soup.findAll("p"):   
     print node 
     #None of two lines are working 
     #text.join(node) 
     #text += node 

这应该工作:

text += str(node) 
+0

问题是我想不仅是文字,但我需要的一切,完整的标签为字符串。 – FootAdministration

+0

@FootAdministration so .. str(node)? –

+0

我需要'节点'转换为字符串和'节点'是一个标签,我不想从标签中提取文本。 – FootAdministration