[英]Extract a certain content from html using python BeautifulSoup
[英]Python: Need to extract tag content from html page using regex, but not BeautifulSoup
我有一个要求,我必须在<raw>
标记内提取内容。 例如,我需要从这个 html 片段中提取abcd
和efgh
:
<html><body><raw somestuff>abcd</raw><raw somesuff>efgh</raw></body></html>
我在我的 python 中使用了这个代码re.match(r'.*raw.*(.*)/raw.*', DATA)
但这不会返回任何 substring。 我不擅长正则表达式。 因此,对此进行更正或新的解决方案将对我有很大帮助。 我不应该使用外部库(由于我公司的一些限制)。
您的公司确实需要重新考虑他们的政策。 重写 XML 解析器完全是浪费时间,Python 已经有好几个了。 Some are included in the stdlib, so if you can import re
you should also be allowed to import xml.etree.ElementTree
or anything else listed at http://docs.python.org/library/markup.html .
你真的应该使用其中之一。 重复所有这些工作是没有意义的。
至少对于您的示例,使用非贪婪匹配 (*?) 可以轻松做到这一点。
re.findall(r'<raw[^>]*?>(.*?)</raw>', DATA)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.