我在HTML文档中有一个固定元素,我需要得到它的位置:
这是什么方法?
我尝试过:
from bs4 import BeautifulSoup
markup = open("myFile.html")
soup = BeautifulSoup(markup=markup.read(), features='html.parser')
markup.close()
spans = soup.find_all('span')
for sp in spans:
print(sp.get('style'))
它返回了None
元素:
<span class="ocrx_word" id="word_1_304" title="bbox 1459 1183 1505 1205; x_wconf 77" contenteditable="true" style="font-family: sans-serif; position: fixed; left: 1459px; top: 1183px; width: 46px; height: 22px;">DC</span>
位置:
element.style {
font-family: sans-serif;
position: fixed;
left: 1459px;
top: 1183px;
width: 46px;
height: 22px;
}
答案 0 :(得分:0)
使用css selector
并搜索span tag with style
属性。
from bs4 import BeautifulSoup
html='''<span class="ocrx_word" id="word_1_304" title="bbox 1459 1183 1505 1205; x_wconf 77" contenteditable="true" style="font-family: sans-serif; position: fixed; left: 1459px; top: 1183px; width: 46px; height: 22px;">DC</span>'''
soup=BeautifulSoup(html,"html.parser")
for item in soup.select("span[style]"):
print(item['style'])