gpt4 book ai didi

python - 解析除以
但不在 内的文本

转载 作者:行者123 更新时间:2023-11-28 03:07:05 29 4
gpt4 key购买 nike

我不知道如何解析这种类型的数据:

<div id="tabs-1" class="ui-tabs-panel ui-widget-content ui-corner-bottom">



<strong><span itemprop="name">MOS-SCHAUM</span></strong><br>
<span itemprop="description">Antistatická pena čierna na IO 300x300x6mm</span>


<br>RoHS: Áno
<br>Obj.číslo: 13291<br>



</div>

可以有很多<span>片段内的标签 - 我不想得到它们。我只想要那些不在里面的 <span>标签。

所以结果是:

{'RoHS':'Áno',
'Obj.číslo': '13291'}

我正在考虑 .contents但很难预测哪些元素会出现在哪个索引上。

你知道怎么做吗?

编辑:即使我尝试这样做:

detail_table = soup.find('div',id="tabs-1")                              
itemprops = detail_table.find_all('span',itemprop=re.compile('.+'))
for item in itemprops:
data[item['itemprop']]=item

contents = detail_table.contents[-1].contents[-1].contents[-1].contents

for i,c in enumerate(contents):
print c
print '---'

我明白了:

RoHS: Áno           
# 1st element
---
<br>Obj.�íslo: 68664<br>
</br></br> # 2st element
---

EDIT2:我刚找到一个解决方案,但不是很好。必须有更优雅的解决方案:

def get_data(url):                                                                 
data = {}
soup = get_soup(url)

""" TECHNICAL INFORMATION """
tech_par_table = soup.find('div',id="tabs-2")
trs = tech_par_table.find_all('tr')
for tr in trs:
tds = tr.find_all('td')
parameter = tds[0].text
value = tds[1].text
data[parameter]=value

""" DETAIL """
detail_table = soup.find('div',id="tabs-1")
itemprops = detail_table.find_all('span',itemprop=re.compile('.+'))
for item in itemprops:
data[item['itemprop'].replace('\n','').replace('\t','').strip()]=item.text.

contents = detail_table.contents[-1].contents[-1].contents[-1].contents

for i,c in enumerate(contents):
if isinstance(c,bs4.element.NavigableString):
splitted = c.split(':')
data[splitted[0]]=splitted[1].replace('\n','').replace('\t','').strip()
if isinstance(c,bs4.element.Tag):
splitted = c.text.split(':')
data[splitted[0]]=splitted[1].replace('\n','').replace('\t','').strip()

最佳答案

首先你需要得到所有的br标签并使用.next_element属性以在每个 br 标记之后立即获取解析的内容;这是您的文字。

d = {}

for br in soup.find_all('br'):
text = br.next_element.strip()
if text:
arr = text.split(':')
d[arr[0]] = arr[1].strip()
print(d)

产量:

{'Obj.číslo': '13291', 'RoHS': 'Áno'}

关于python - 解析除以 <br> 但不在 <span> 内的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32165026/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com