通过样式定位标签 - 使用 Python 2 和 BeautifulSoup 4
Locating tags via styles - using Python 2 and BeautifulSoup 4
我正在尝试使用 BeautifulSoup 4 从 HTML 文档中的特定标签中提取文本。我有 HTML 有一堆 div 标签,如下所示:
<div style="position:absolute; border: textbox 1px solid; writing-mode:lr-tb; left:42px; top:90px; width:195px; height:24px;">
<span style="font-family: FIPXQM+Arial-BoldMT; font-size:12px">
Futures Daily Market Report for Financial Gas
<br/>
21-Jul-2015
<br/>
</span>
</div>
<div style="position:absolute; border: textbox 1px solid; writing-mode:lr-tb; left:54px; top:135px; width:46px; height:10px;">
<span style="font-family: FIPXQM+Arial-BoldMT; font-size:10px">
COMMODITY
<br/>
</span>
</div>
我正在尝试从具有 "left:54px" 样式的任何 div 标签中的所有 span 标签中获取文本。
我可以得到一个 div 如果我使用:
soup = BeautifulSoup(open(extracted_html_file))
print soup.find_all('div',attrs={"style":"position:absolute; border: textbox 1px solid; "
"writing-mode:lr-tb; left:42px; top:90px; "
"width:195px; height:24px;"})
它returns:
[<div style="position:absolute; border: textbox 1px solid; writing-mode:lr-tb; left:42px; top:90px; width:195px; height:24px;"><span style="font-family: FIPXQM+Arial-BoldMT; font-size:12px">Futures Daily Market Report for Financial Gas
<br/>21-Jul-2015
<br/></span></div>]
但这只会让我得到 div 与该样式完全匹配的那个。我想要所有仅匹配 "left:54px" 样式的 div。
为此,我尝试了几种不同的方法:
soup = BeautifulSoup(open(extracted_html_file))
print soup.find_all('div',style='left:54px')
print soup.find_all('div',attrs={"style":"left:54px"})
print soup.find_all('div',attrs={"left":"54px"})
但是所有这些打印语句 return 空列表。
有什么想法吗?
您可以根据此处的文档传入正则表达式而不是字符串:http://www.crummy.com/software/BeautifulSoup/bs4/doc/#the-keyword-arguments
所以我会试试这个:
import re
soup = BeautifulSoup(open(extracted_html_file))
soup.find_all('div', style = re.compile('left:54px'))
我正在尝试使用 BeautifulSoup 4 从 HTML 文档中的特定标签中提取文本。我有 HTML 有一堆 div 标签,如下所示:
<div style="position:absolute; border: textbox 1px solid; writing-mode:lr-tb; left:42px; top:90px; width:195px; height:24px;">
<span style="font-family: FIPXQM+Arial-BoldMT; font-size:12px">
Futures Daily Market Report for Financial Gas
<br/>
21-Jul-2015
<br/>
</span>
</div>
<div style="position:absolute; border: textbox 1px solid; writing-mode:lr-tb; left:54px; top:135px; width:46px; height:10px;">
<span style="font-family: FIPXQM+Arial-BoldMT; font-size:10px">
COMMODITY
<br/>
</span>
</div>
我正在尝试从具有 "left:54px" 样式的任何 div 标签中的所有 span 标签中获取文本。
我可以得到一个 div 如果我使用:
soup = BeautifulSoup(open(extracted_html_file))
print soup.find_all('div',attrs={"style":"position:absolute; border: textbox 1px solid; "
"writing-mode:lr-tb; left:42px; top:90px; "
"width:195px; height:24px;"})
它returns:
[<div style="position:absolute; border: textbox 1px solid; writing-mode:lr-tb; left:42px; top:90px; width:195px; height:24px;"><span style="font-family: FIPXQM+Arial-BoldMT; font-size:12px">Futures Daily Market Report for Financial Gas
<br/>21-Jul-2015
<br/></span></div>]
但这只会让我得到 div 与该样式完全匹配的那个。我想要所有仅匹配 "left:54px" 样式的 div。
为此,我尝试了几种不同的方法:
soup = BeautifulSoup(open(extracted_html_file))
print soup.find_all('div',style='left:54px')
print soup.find_all('div',attrs={"style":"left:54px"})
print soup.find_all('div',attrs={"left":"54px"})
但是所有这些打印语句 return 空列表。
有什么想法吗?
您可以根据此处的文档传入正则表达式而不是字符串:http://www.crummy.com/software/BeautifulSoup/bs4/doc/#the-keyword-arguments
所以我会试试这个:
import re
soup = BeautifulSoup(open(extracted_html_file))
soup.find_all('div', style = re.compile('left:54px'))