需要使用 BeautifulSoup 提取标签中的所有字符或将完整标签转换为字符串
Need to extract all characters in a tag with BeautifulSoup or turn full tag into string
我需要将 Tag
对象或 'node' 转换为字符串。
这是我的代码:
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)
我需要将 Tag
对象或 'node' 转换为字符串。
这是我的代码:
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)