【问题标题】:Need to extract all characters in a tag with BeautifulSoup or turn full tag into string [closed]需要使用 BeautifulSoup 提取标签中的所有字符或将完整标签转换为字符串 [关闭]
【发布时间】:2016-06-07 07:49:55
【问题描述】:

我需要将Tag 对象或“节点”转换为字符串。 这是我的代码:

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

【问题讨论】:

    标签: python beautifulsoup text-extraction


    【解决方案1】:

    这应该可行:

    text += str(node)
    

    【讨论】:

    • 问题是我不仅想要文本,还需要所有内容,完整的标签作为字符串。
    • @FootAdministration so.. str(node)?
    • 我需要将 'node' 转换为字符串,而 'node' 是一个标签,我不想从标签中提取文本。
    • 感谢 H. Lewroll!
    • @FootAdministration 如果它足够好,你应该接受答案
    猜你喜欢
    • 2019-08-12
    • 1970-01-01
    • 2021-11-07
    • 2021-01-24
    • 2021-01-29
    • 1970-01-01
    • 2016-02-04
    • 1970-01-01
    • 2015-06-27
    相关资源
    最近更新 更多