Python Can';不要解析注释中的某些文本

Python Can';不要解析注释中的某些文本,python,python-3.x,web-scraping,beautifulsoup,Python,Python 3.x,Web Scraping,Beautifulsoup,我试图解析下面片段中注释中的内容,但似乎根本不起作用。我怎样才能让它工作?我的目的是获取p标记中的文本,输出应该是: Hi there!! Hi again!! 我已经尝试过的脚本: from bs4 import BeautifulSoup, Comment content=""" <!-- comment --><a href="https://extratorrent.ag/"><p>Hi there!!</p></a> &l

我试图解析下面片段中注释中的内容,但似乎根本不起作用。我怎样才能让它工作?我的目的是获取
p
标记中的文本,输出应该是:

Hi there!!
Hi again!!
我已经尝试过的脚本:

from bs4 import BeautifulSoup, Comment

content="""
<!-- comment --><a href="https://extratorrent.ag/"><p>Hi there!!</p></a>
<!-- comment1 --><a href="https://thepiratebay.se/"><p>Hi again!!</p></a>
"""
soup = BeautifulSoup(content, 'lxml')
for comment in soup.find_all(string=lambda text:isinstance(text,Comment)):
    data = BeautifulSoup(comment.next_element,"lxml")
    for item in data.select("p"):
        print(item.text)
来自bs4导入美化组的
,注释
content=”“”
"""
汤=美汤(内容为“lxml”)
查找所有(字符串=lambda text:isinstance(text,comment)):
数据=美化组(注释。下一个元素“lxml”)
对于数据中的项目,选择(“p”):
打印(项目.文本)
我的错误是:

Traceback (most recent call last):
  File "C:\AppData\Local\Programs\Python\Python35-32\Social.py", line 9, in <module>
    data = BeautifulSoup(comment.next_element,"lxml")
  File "C:\AppData\Local\Programs\Python\Python35-32\lib\site-packages\bs4\__init__.py", line 191, in __init__
    markup = markup.read()
TypeError: 'NoneType' object is not callable
回溯(最近一次呼叫最后一次):
文件“C:\AppData\Local\Programs\Python\Python35-32\Social.py”,第9行,在
数据=美化组(注释。下一个元素“lxml”)
文件“C:\AppData\Local\Programs\Python\Python35-32\lib\site packages\bs4\\ uuuuuu init\uuuuuu.py”,第191行,在\uuuu init中__
markup=markup.read()
TypeError:“非类型”对象不可调用

切换到
html.parser
,然后只需访问内部的
p
标记即可

html.parser
的优点是它不会在数据周围添加额外的
..
标记。然后,您可以使用
comment.next_element.p.text
访问
p
标记的内容

soup = BeautifulSoup(content, 'html.parser')
for comment in soup.find_all(string=lambda text: isinstance(text, Comment)):
    print(comment.next_element.p.text)

Hi there!!
Hi again!!

发布错误的完整回溯哇!!!这是一个传奇的修复。系统不允许我在时间到来之前接受你的回答。已经有一个加号了。非常感谢。@coder新手不客气!尽管您可能希望确保当锚标记或p标记未在注释后面时,代码不会出错。(想想,
试试……除了
)祝你好运!