【发布时间】:2016-11-09 14:46:59
【问题描述】:
我正在尝试获取元标记的内容。问题是 BS4 无法在某些网站上正确解析标签,标签没有按应有的方式关闭。使用以下示例中的标签,我的函数的输出包括大量杂乱无章的内容,包括脚本、链接等其他标签。我相信浏览器会自动关闭头部末尾的元标签,这种行为会让 BS4 感到困惑。
我的代码适用于此:
<meta name="description" content="content" />
并且不适用于:
<meta name="description" content="content">
这是我的 BS4 函数的代码:
from bs4 import BeautifulSoup
html = BeautifulSoup(open('/path/file.html'), 'html.parser')
desc = html.find(attrs={'name':'description'})
print(desc)
有什么方法可以让它与那些未关闭的元标记一起工作?
【问题讨论】:
标签: python python-3.x beautifulsoup html-parsing