Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.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 如何使用Beautiful Soup获得元素的位置?_Python_Html_Web_Beautifulsoup - Fatal编程技术网

Python 如何使用Beautiful Soup获得元素的位置?

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=

我在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=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它改变了什么吗?因为它恰好在同一个位置,我不认为它改变了什么。我只是好奇你为什么这么做。