Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.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-各种情况_Python_Regex_Beautifulsoup - Fatal编程技术网

Python 正则表达式或BeautifulSoup-各种情况

Python 正则表达式或BeautifulSoup-各种情况,python,regex,beautifulsoup,Python,Regex,Beautifulsoup,我有3个字符串要检索,它们的特征是有两个词:section和front。我对regex很反感 contentFrame wsj-sectionfront economy_sf contentFrame wsj-sectionfront business_sf section-front markets 如何使用一个正则表达式匹配这两个单词?这将用于匹配由BeautifulSoup解析的html页面的内容 更新: 我想提取具有div标记的网页()的主体:。出于某些原因,BeautifulSoup

我有3个字符串要检索,它们的特征是有两个词:
section
front
。我对regex很反感

contentFrame wsj-sectionfront economy_sf
contentFrame wsj-sectionfront business_sf
section-front markets
如何使用一个正则表达式匹配这两个单词?这将用于匹配由BeautifulSoup解析的html页面的内容

更新:

我想提取具有
div
标记的网页()的主体:。出于某些原因,BeautifulSoup无法通过以下方式识别高亮显示的类属性:

wsj_soup.find('div', attrs = {'class':'contentFrame wsj-sectionfront business_sf')
# Returns []

我正尽可能多地呆在BeautifulSoup,但如果regex是我的选择,我会用它。从这里开始,我很可能会使用
contents
属性搜索相关关键字,但如果有人对如何搜索有更好的想法,请分享。

处理此问题的一种方法是使用两个单独的lookahead来检查每一个单词:

^(?=.*section)(?=.*front).*$

如果它是BeautifulSoup,而你不知道正则表达式,为什么不坚持你知道的东西,并传递一个
lambda文本:文本中的“section”和文本中的“front”
到BeautifulSoup?@Aran Fey工作得很好,但我无法提取我需要的东西(这是我的错,因为使用
section
front
的类属性比我预期的多得多)。请继续关注更新。