从 Python 中的 HTML 页面中的特定 div 导出值



我有多个div的HTML页面,称为"rptTopics_rptTopicTechnics_[X]_lblTechnicalValue_0" 每个 X 都有不同的值 HTML代码示例:

<div class="specific_info_txt">
<span 
id="rptTopics_rptTopicTechnics_10_lblTechnicalValue_0" dir="ltr">Starting: 
2.kg</span>
</div>

我希望代码运行并找到包含字符串"kg"的特定div

我做到了:

def get_weight(desc):
a = 1
for span in desc:
weight = sec.find_element_by_id('rptTopics_rptTopicTechnics_' + str(a) + '_lblTechnicalValue_0').text
if ('kg' in weight or 'ק"ג' in weight or 'KG' in weight or 'Kg' in weight):
Fweight  = weight
break
else:
a += 1
return Fweight  

那行不通...阅读所有div并获得正确div的正确方法是什么?

您可以使用驱动程序函数find_elements_by_xpath以及将为您找到这些元素的XPath。例如:

def get_weight():
# Find all the spans that are under the div with class 'specific_info_txt'
# and the children span contains the id 'rptTopics_rptTopicTechnics_'
spans = sec.find_elements_by_xpath("*//div[@class,'specific_info_txt']/span[contains(@id,'rptTopics_rptTopicTechnics_')"]
# In case that for some reason the weight is not found in the page
Fweight = None
# Loop over the found spans
for span in spans:
weight = span.text
if ('kg' in weight or 'ק"ג' in weight or 'KG' in weight or 'Kg' in weight):
Fweight  = weight
break
return Fweight

相关内容

  • 没有找到相关文章

最新更新