英文:
How to extract specific data from HTML?
问题
这个代码的问题在于它假定"✅"符号总是出现在Jogos时间列表中,并且总是在第一个Jogo时间后。但实际情况可能是"✅"符号出现在任何一个Jogo时间后,也可能没有任何"✅"符号。
要解决这个问题,你可以使用以下方式来提取数据:
from bs4 import BeautifulSoup
html = """
<div class="body">
<div class="pull_right date details" title="09.03.2023 01:08:10 UTC-03:00">
01:08
</div>
<div class="from_name">
🤖🥇 𝑬𝒂𝒔𝒚 𝑩𝒐𝒕 - 𝑶𝒗𝒆𝒓 2.5
</div>
<div class="text">
Easy Bot - Over 2.5<br><br>⚽ Liga: Sul-Americana<br>⚽ Entrada: Over 2.5 FT<br>⚽ Jogos: ✅ 04:10 04:13 04:16 (04:19)<br><br><strong>Link: </strong><a href="https://www.bet365.com/#/AVR/B146/R%5E1/">https://www.bet365.com/#/AVR/B146/R%5E1/</a><br><br>24h:100% de acerto nas últimas 24h<br><br>✅✅✅✅✅✅ .
</div>
</div>
"""
# 解析HTML
soup = BeautifulSoup(html, 'html.parser')
d_str = soup.select_one('div.date.details')['title']
calendar = d_str.split(" ")
print("Date:", calendar[0])
print("Time:", calendar[1])
text_div = soup.select_one('div.text')
for sts in text_div.stripped_strings:
if "Jogos: ✅" in sts:
jogos_info = sts.split("Jogos: ✅")[1]
jogos = jogos_info.split()
checkmarked = jogos.index('04:10') + 1 if '04:10' in jogos else 0
print("Jogo 1:", jogos[0])
print("Checkmarked:", checkmarked)
这段代码会尝试从文本中提取Jogos时间信息,并找出"✅"符号的位置。如果没有"✅"符号,它会将Checkmarked设置为0。这样,无论"✅"符号出现在哪个Jogo时间后,都能正确提取数据。
英文:
I need to extract from HTML the following data: Date, Time, the first Jogo and, if the checkmark symbol is present on the first, second, third or fourth time or display 0 if there's no checkmark.
The returns fine all data when the checkmark is present, but when there's no checkmark it just displays the Date and Time.
My python code is:
from bs4 import BeautifulSoup
html = """
<div class="body">
<div class="pull_right date details" title="09.03.2023 01:08:10 UTC-03:00">
01:08
</div>
<div class="from_name">
🤖🥇 𝑬𝒂𝒔𝒚 𝑩𝒐𝒕 - 𝑶𝒗𝒆𝒓 2.5
</div>
<div class="text">
Easy Bot - Over 2.5<br><br>🏆 Liga: Sul-Americana<br>🚦 Entrada: Over 2.5 FT<br>⚽ Jogos: ✅ 04:10 04:13 04:16 (04:19)<br><br><strong>Link: </strong><a href="https://www.bet365.com/#/AVR/B146/R%5E1/">https://www.bet365.com/#/AVR/B146/R%5E1/</a><br><br>🍀 24h:100% de acerto nas últimas 24h<br><br>✅✅✅✅✅✅ .
</div>
</div>
"""
# parse the HTML
soup = BeautifulSoup(html, 'html.parser')
d_str = soup.select_one('div.date.details')['title']
calendar = d_str.split(" ")
print("Date: ",calendar[0])
print("Time: ",calendar[1])
for sts in soup.select_one('div.text').stripped_strings:
if "⚽ Jogos: " in sts:
jugos = (sts.split('⚽ Jogos: ')[1].split(" "))
ind = jugos.index('✅')+1
jugos.remove("✅")
print("Jogo 1: ", jugos[0])
print("Checkmarked: ", ind)
Note that the checkmark "belongs" to the time. So it can be on the first, second, third or fourth Jogos time.
But sometimes there will be no checkmark. when this happens the output should be:
Date: XXX
Time: XXX
Jogo 1: XXX
Checkmarked: 0
So, what's the problem with this code?
答案1
得分: 1
我会这样做:
d_str = soup.select_one('div.date.details')['title']
calendar = d_str.split(" ")
print("日期: ", calendar[0])
print("时间: ", calendar[1])
for sts in soup.select_one('div.text').stripped_strings:
if "⚽ Jogos: " in sts:
jogos = (sts.split('⚽ Jogos: ')[1].split(" "))
if "✅" in jogos:
ind = jogos.index('✅')+1
print("已标记: ", ind)
jogos.remove("✅")
print(jogos)
else:
print(jogos)
print("已标记: 无")
带有检查标记的输出:
日期: 09.03.2023
时间: 01:08:10
['04:10', '04:13', '04:16', '(04:19)']
已标记: 1
没有检查标记的输出:
日期: 09.03.2023
时间: 01:08:10
['04:10', '04:13', '04:16', '(04:19)']
已标记: 无
当然,你可以将输出添加到列表等其他数据结构中,而不是直接打印出来。
英文:
I would do it this way:
d_str = soup.select_one('div.date.details')['title']
calendar = d_str.split(" ")
print("Date: ",calendar[0])
print("Time: ",calendar[1])
for sts in soup.select_one('div.text').stripped_strings:
if "⚽ Jogos: " in sts:
jugos = (sts.split('⚽ Jogos: ')[1].split(" "))
if "✅" in jugos:
ind = jugos.index('✅')+1
print("Checkmarked: ", ind)
jugos.remove("✅")
print(jugos)
else:
print(jugos)
print("Checkmarked: NA")
Output with check mark:
Date: 09.03.2023
Time: 01:08:10
['04:10', '04:13', '04:16', '(04:19)']
Checkmarked: 1
Output without check mark:
Date: 09.03.2023
Time: 01:08:10
['04:10', '04:13', '04:16', '(04:19)']
Checkmarked: NA
Of course, instead of printing you can add the output to a list, etc.
答案2
得分: 0
不确定预期结果应该如何或有多少项需要迭代,因此此代码应该指向一个方向。
问题在于string='⚽ Jogos:'
需要完全匹配。
示例:
from bs4 import BeautifulSoup
html = '''
<div class="body">
<div class="pull_right date details" title="09.03.2023 01:08:10 UTC-03:00">
01:08
</div>
<div class="from_name">
🤖🥇 𝑬𝒂𝒔𝒚 𝑩𝒐𝒕 - 𝑶𝒗𝒆𝒓 2.5
</div>
<div class="text">
Easy Bot - Over 2.5<br><br>🏆 Liga: Sul-Americana<br>🚦 Entrada: Over 2.5 FT<br>⚽ Jogos: ✅ 04:10 04:13 04:16 (04:19)<br><br><strong>Link: </strong><a href="https://www.bet365.com/#/AVR/B146/R%5E1/">https://www.bet365.com/#/AVR/B146/R%5E1/</a><br><br>🍀 24h:100% de acerto nas últimas 24h<br><br>✅✅✅✅✅✅ .
</div>
</div>
'''
soup = BeautifulSoup(html)
data = []
for e in soup.select('.text'):
for s in e.stripped_strings:
if 'Jogos' in s:
s=s.split()[2:]
jogo_times = [t for t in s if '✅' not in t]
jogo_check = 展开收缩 for i,t in enumerate(s) if '✅' in t]
d = {
'date':e.find_previous('div',{'class':'date'}).get('title')[:10],
'time':e.find_previous('div',{'class':'date'}).get_text(strip=True),
'jogo_times':jogo_times,
'jogo_time_checked':jogo_check
}
break
if d:
data.append(d)
d = None
else:
print('no jogo')
data
输出:
[{'date': '09.03.2023',
'time': '01:08',
'jogo_times': ['04:10', '04:13', '04:16', '(04:19)'],
'jogo_time_checked': ['04:10']}]
英文:
Not sure how the expected result should look like or how many items are there to iterate, so this code should point into a direction.
Issue here is that string='⚽ Jogos:'
needs an exact match.
Example
from bs4 import BeautifulSoup
html = '''
<div class="body">
<div class="pull_right date details" title="09.03.2023 01:08:10 UTC-03:00">
01:08
</div>
<div class="from_name">
🤖🥇 𝑬𝒂𝒔𝒚 𝑩𝒐𝒕 - 𝑶𝒗𝒆𝒓 2.5
</div>
<div class="text">
Easy Bot - Over 2.5<br><br>🏆 Liga: Sul-Americana<br>🚦 Entrada: Over 2.5 FT<br>⚽ Jogos: ✅ 04:10 04:13 04:16 (04:19)<br><br><strong>Link: </strong><a href="https://www.bet365.com/#/AVR/B146/R%5E1/">https://www.bet365.com/#/AVR/B146/R%5E1/</a><br><br>🍀 24h:100% de acerto nas últimas 24h<br><br>✅✅✅✅✅✅ .
</div>
</div>
'''
soup = BeautifulSoup(html)
data = []
for e in soup.select('.text'):
for s in e.stripped_strings:
if 'Jogos' in s:
s=s.split()[2:]
jogo_times = [t for t in s if '✅' not in t]
jogo_check = 展开收缩 for i,t in enumerate(s) if '✅' in t]
d = {
'date':e.find_previous('div',{'class':'date'}).get('title')[:10],
'time':e.find_previous('div',{'class':'date'}).get_text(strip=True),
'jogo_times':jogo_times,
'jogo_time_checked':jogo_check
}
break
if d:
data.append(d)
d = None
else:
print('no jogo')
data
Output
[{'date': '09.03.2023',
'time': '01:08',
'jogo_times': ['04:10', '04:13', '04:16', '(04:19)'],
'jogo_time_checked': ['04:10']}]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论