Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将数据另存为新行,但保存在单个单元格中lxml python_Python_Beautifulsoup_Lxml - Fatal编程技术网

将数据另存为新行,但保存在单个单元格中lxml python

将数据另存为新行,但保存在单个单元格中lxml python,python,beautifulsoup,lxml,Python,Beautifulsoup,Lxml,我想要这样的数据 “基本运动衫 照罐头上写的做 主营:100%纯棉。” 在一个单元格中,但我得到的数据如下 “基本运动衫符合tinMain上的说明:100%纯棉。” 这是HTML <div class="about-me"> <h4>ABOUT ME</h4> <span><div>Basic jersey</div><div>Does what it says on the tin</di

我想要这样的数据

“基本运动衫

照罐头上写的做

主营:100%纯棉。”

在一个单元格中,但我得到的数据如下

“基本运动衫符合tinMain上的说明:100%纯棉。”

这是HTML

<div class="about-me">
    <h4>ABOUT ME</h4>
    <span><div>Basic jersey</div><div>Does what it says on the tin</div><br>Main: 100% Cotton.</span>
</div>

使用您提供的HTML,您可以使用生成器解决此问题,如下所示:

from bs4 import BeautifulSoup

html = """
<div class="about-me">
    <h4>ABOUT ME</h4>
    <span><div>Basic jersey</div><div>Does what it says on the tin</div><br>Main: 100% Cotton.</span>
</div>"""

soup = BeautifulSoup(html, "html.parser")

print('\n'.join(soup.span.stripped_strings))

您正在解析的HTML是什么?请回答您的问题,并附上相关内容。链接是没有帮助的,因为它们可能会中断,然后问题变得不可用。快速猜测:在XML和HTML中不保留空格,这就是为什么任何换行符都显示为简单空格。除非文本中有像
s这样的元素,否则你就倒霉了。@Robert我已经编辑并添加了HTML,请你检查一下,提前谢谢。那么是什么阻止你查找

s呢?这正是我需要的,非常感谢much@Martin埃文斯
from bs4 import BeautifulSoup

html = """
<div class="about-me">
    <h4>ABOUT ME</h4>
    <span><div>Basic jersey</div><div>Does what it says on the tin</div><br>Main: 100% Cotton.</span>
</div>"""

soup = BeautifulSoup(html, "html.parser")

print('\n'.join(soup.span.stripped_strings))