可变长度文本后的正则表达式搜索
Regex Search after a variable length text
我需要正则表达式来从以下标签中提取文本:
我正在使用 Python & BeautifulSoup
<h4 style="color:#000000; line-height:20px; font-size:18px; margin-left:22px;
overflow:auto; content:inherit; padding:10px; font-family:"Book Antiqua",
Palatino, serif;">THE TEXT TO BE EXTRACTED IS HERE</h4></div><br /></div>
我尝试了以下方法:
stylecontent = 'color:#000000; line-height:20px; font-size:18px; margin-left:22px;
overflow:auto; content:inherit; padding:10px; font-family:"Book Antiqua",
Palatino, serif;'
soup = BeautifulSoup(br.response().read(), "lxml")
scrap_soup = soup.findAll('h4', {'style': stylecontent})
但它并不总是有效,因为网站不断变化 stylecontent
。
现在我想使用正则表达式:
soup.find_all(re.compile("some_foo_regex")):
我对那个感兴趣some_foo_regex
。
谢谢。
您可能会得到所有只有一个属性 style
和
的 h4
标签
h4_tags = soup.find_all('h4', attrs = {'style' : True}) # Get all H4 tags with style attribute
for result in h4_tags:
if len(result.attrs) == 1: # Print if it is the only attribute
print result.contents # Print tag text contents
我需要正则表达式来从以下标签中提取文本: 我正在使用 Python & BeautifulSoup
<h4 style="color:#000000; line-height:20px; font-size:18px; margin-left:22px;
overflow:auto; content:inherit; padding:10px; font-family:"Book Antiqua",
Palatino, serif;">THE TEXT TO BE EXTRACTED IS HERE</h4></div><br /></div>
我尝试了以下方法:
stylecontent = 'color:#000000; line-height:20px; font-size:18px; margin-left:22px;
overflow:auto; content:inherit; padding:10px; font-family:"Book Antiqua",
Palatino, serif;'
soup = BeautifulSoup(br.response().read(), "lxml")
scrap_soup = soup.findAll('h4', {'style': stylecontent})
但它并不总是有效,因为网站不断变化 stylecontent
。
现在我想使用正则表达式:
soup.find_all(re.compile("some_foo_regex")):
我对那个感兴趣some_foo_regex
。
谢谢。
您可能会得到所有只有一个属性 style
和
h4
标签
h4_tags = soup.find_all('h4', attrs = {'style' : True}) # Get all H4 tags with style attribute
for result in h4_tags:
if len(result.attrs) == 1: # Print if it is the only attribute
print result.contents # Print tag text contents