Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.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/3/html/91.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
Python 如何从多个文档中提取<;span>;使用BS4标记和分组数据?_Python_Html_Beautifulsoup - Fatal编程技术网

Python 如何从多个文档中提取<;span>;使用BS4标记和分组数据?

Python 如何从多个文档中提取<;span>;使用BS4标记和分组数据?,python,html,beautifulsoup,Python,Html,Beautifulsoup,我从一个网页中提取了基于其类的span标记之间的数据。但有时,网页会将一行分割成多个片段,并将其存储在连续的标签中。所有子span标记都具有相同的类名 以下是HTML代码段: <p class="Paragraph SCX"> <span class="TextRun SCX"> <span class="NormalTextRun SCX"> This week </span>

我从一个网页中提取了基于其类的span标记之间的数据。但有时,网页会将一行分割成多个片段,并将其存储在连续的标签中。所有子span标记都具有相同的类名

以下是HTML代码段:

<p class="Paragraph SCX">
    <span class="TextRun SCX">
        <span class="NormalTextRun SCX">
            This week
        </span>
    </span>
    <span class="TextRun SCX">
        <span class="NormalTextRun SCX">
            &nbsp;(12/
        </span>
    </span>
    <span class="TextRun SCX">
        <span class="NormalTextRun SCX">
            11
        </span>
    </span>
    <span class="TextRun SCX">
        <span class="NormalTextRun SCX">
            &nbsp;- 12/1
        </span>
    </span>
    <span class="TextRun SCX">
        <span class="NormalTextRun SCX">
            7
        </span>
    </span>
    <span class="TextRun SCX">
        <span class="NormalTextRun SCX">
            ):
        </span>
    </span>
    <span class="EOP SCX">
        &nbsp;
    </span>
</p>
此代码导致每个数据作为单独的实体单独打印。 所需输出:

本周(11月12日-17月12日):


知道如何将这些span标记数据组合在一起吗?谢谢!

您可以尝试使用join方法将相关信息组合在一个字符串中

dates = '' 
for data in elem.find_all('span', class_='TextRun'):
    dates.join([dates, data.text])

试一试。确保将整个
html
包装在
content
变量中

from bs4 import BeautifulSoup
soup = BeautifulSoup(content,'lxml')
data = ''.join([' '.join(item.text.split()) for item in soup.select(".NormalTextRun")])
print(data)
输出:

This week(12/11- 12/17):

仅将NormalTextRun类添加到元素中数据的forloop中。find_all('span',class=“NormalTextRun”):获取列表中的所有字符串并将其连接()。@AnupYadav:这会引发以下错误:a=data.find('span')。contents[0]AttributeError:“非类型”对象没有属性“内容”是的,您现在不需要在span中,您已经在较低的span中,需要替换为a=data.find('span')。contents[0]到a=data.contents[0]@Shahin-谢谢您的回答!我还有一点疑问:我将上述HTML内容包装在列表标记中(
  • )还有多个这样的
  • 标记,我从中读取内容。有没有办法在提取每个
  • 标记数据后添加逗号之类的分隔符?这样我就可以将每个内容存储为列表元素。预期输出:[本周(12/11-12/17):,XYZ,ABC]
    This week(12/11- 12/17):