Python 如何使用Beautiful Soup获得元素的位置?
我在HTML文档中有一个固定元素,我需要得到它的位置:Python 如何使用Beautiful Soup获得元素的位置?,python,html,web,beautifulsoup,Python,Html,Web,Beautifulsoup,我在HTML文档中有一个固定元素,我需要得到它的位置: element.style { font-family: sans-serif; position: fixed; left: 1459px; top: 1183px; width: 46px; height: 22px; } 它的方法是什么 我试过: from bs4 import BeautifulSoup markup = open("myFile.html") soup = BeautifulSoup(markup=
element.style {
font-family: sans-serif;
position: fixed;
left: 1459px;
top: 1183px;
width: 46px;
height: 22px;
}
它的方法是什么
我试过:
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>
使用
css选择器
并使用style属性搜索span标记
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'])
从bs4导入美化组
html=''DC''
soup=BeautifulSoup(html,“html.parser”)
对于汤中的项目。选择(“span[style]”:
打印(项目['style'])
为什么要将标记作为关键字arg传递?可以使用类似sp[“style”]
的方法。这应该行。@ArmedinKuka我得到了错误KeyError:'style'
@JohnGordon它改变了什么吗?因为它恰好在同一个位置,我不认为它改变了什么。我只是好奇你为什么这么做。