Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.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 BeautifulSoup在HTML代码中查找特定注释条目_Python_Beautifulsoup_Comments_Html Parsing - Fatal编程技术网

使用python BeautifulSoup在HTML代码中查找特定注释条目

使用python BeautifulSoup在HTML代码中查找特定注释条目,python,beautifulsoup,comments,html-parsing,Python,Beautifulsoup,Comments,Html Parsing,我试图从HTML解析评论会话中的特定条目。我正试图用BeautifulSoup来做这件事。在我能够提取评论部分后,我被卡住了。下面是一个例子: 你们知道我怎样才能得到正确的信息吗?谢谢你 我还没有看到所有html文件的内容,但由于所有注释的格式都相同,也许您可以将它们直接解析为字符串,如下所示: # parse the comments for comment in comments: for line in comment.splitlines(): if ':' i

我试图从HTML解析评论会话中的特定条目。我正试图用BeautifulSoup来做这件事。在我能够提取评论部分后,我被卡住了。下面是一个例子:


你们知道我怎样才能得到正确的信息吗?谢谢你

我还没有看到所有html文件的内容,但由于所有注释的格式都相同,也许您可以将它们直接解析为字符串,如下所示:

# parse the comments
for comment in comments:
    for line in comment.splitlines():
        if ':' in line:
            attrib, val = line.strip().rsplit(':',1)
            print '{} -> {}'.format(attrib, val.strip(','))

所有评论都有这种特定的格式吗?是的,它们的格式相同;但是有多块评论。谢谢
# parse the comments
for comment in comments:
    for line in comment.splitlines():
        if ':' in line:
            attrib, val = line.strip().rsplit(':',1)
            print '{} -> {}'.format(attrib, val.strip(','))