第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

Divs 下的強(qiáng)實(shí)例 - 僅捕獲初始實(shí)例

Divs 下的強(qiáng)實(shí)例 - 僅捕獲初始實(shí)例

慕森卡 2022-06-02 14:35:11
我的代碼有一個(gè)小問題。實(shí)際上,我的目標(biāo)是從一些代碼中獲取以下實(shí)例:<td><div><div>8 of 15 — <strong>53%</strong></div><div><div style="width: 100%"><div style="width: 53%"></div></div></div></div></td>我試圖在 div 中捕獲數(shù)據(jù),在強(qiáng)之前,以及強(qiáng)百分比。目前正在使用以下內(nèi)容獲取這些數(shù)據(jù),但我只能獲取 a) 第一個(gè)實(shí)例或 b) 空值:from bs4 import BeautifulSoup as bsoupimport requests as reqsother_stat_list = []page_to_parse = 'https://fbref.com/en/matches/033092ef/Northampton-Town-Lincoln-City-August-4-2018-League-Two'page = reqs.get(page_to_parse)status_code = page.status_codestatus_code = str(status_code)parse_page = bsoup(page.content, 'html.parser')find_other_stats = parse_page.find_all('div', id="team_stats")for stat in find_other_stats:    add_other_stats = stat.find_next('strong').get_text()    other_stat_list.append(add_other_stats)    print(add_other_stats)我之前遇到過類似的問題,不是所有的 div 實(shí)例都被捕獲。我運(yùn)行了一個(gè)遞歸來捕獲代碼中的所有孩子:find_other_stats = parse_page.find_all('div', id="team_stats")all_other_stats = find_other_stats[0].find_all('div', recursive=False)for stat in all_other_stats:    add_other_stats = find_next('strong').get_text()     other_stat_list.append(add_team)然而,這個(gè)變體也會(huì)產(chǎn)生空值,所以不確定為什么遞歸不起作用。期望得到'53%',理想情況下是'8 of 15 -。我可以自己解析這些值,但捕獲它們看起來比我預(yù)期的要難。謝謝您的幫助!
查看完整描述

1 回答

?
慕斯王

TA貢獻(xiàn)1864條經(jīng)驗(yàn) 獲得超2個(gè)贊

使用此代碼,我將所有部分作為單獨(dú)的元素。


第一:只有一個(gè)divwithid="team_stats"所以我用find()而不是find_all()


而不是 searchigdiv我搜索td并使用get_text()我在單元格中獲取全文。這樣我就不需要遞歸嵌套<div>,也不會(huì)從 empty 得到空字符串<div>。


從表格中的所有單元格中獲取所有文本后,我將其拆分為較小的部分并清理它們。


我將其拆分—以獲取53%和8 of 15作為單獨(dú)的元素。但這—是不正常的-,所以我手動(dòng)將它從 HTML 復(fù)制/粘貼到代碼中。


我發(fā)現(xiàn)還有帶有代碼的字符'\xa0'(可以是“不間斷空格”或類似的東西),我使用它清理它strip('\xa0')- 我也可以使用rstrip()/lstrip()或replace()`` or slice it with[1:] and[:-1]`。


from bs4 import BeautifulSoup as BS

import requests 


url = 'https://fbref.com/en/matches/033092ef/Northampton-Town-Lincoln-City-August-4-2018-League-Two'


response = requests.get(url)

soup = BS(response.content, 'html.parser')


# --- getting data ---


data = []


stats = soup.find('div', id="team_stats")


for row in stats.find_all('td'):

    text = row.get_text(strip=True)

    data.append(text)


# --- splitting and cleaning data ---


print('Possession:', data[0], '|', data[1])


text1, percent1 = data[2].split('—')

percent2, text2 = data[3].split('—')

text1 = text1.strip('\xa0')

text2 = text2.strip('\xa0')

print('Shots on Target:', text1, '|', percent1, '|', text2, '|', percent2)


text1, percent1 = data[4].split('—')

percent2, text2 = data[5].split('—')

text1 = text1.strip('\xa0')

text2 = text2.strip('\xa0')

print('Saves:', text1, '|', percent1, '|', text2, '|', percent2)

結(jié)果:


Possession: 58% | 42%

Shots on Target: 8 of 15 | 53% | 2 of 4 | 50%

Saves: 1 of 2 | 50% | 8 of 8 | 100%


查看完整回答
反對 回復(fù) 2022-06-02
  • 1 回答
  • 0 關(guān)注
  • 139 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)