[英]Extract data between br Tag with beautifulsoup
如何使用beautifulsoup從以下html代碼中提取INFO1
和INFO2
?
網站: https : //swisswrestling.ch/wrestlers? id =91
我也嘗試為戰斗提取數據,但這對我來說適用於 bs4。
<tr>
<td>
<span class="text-danger font-weight-bold"><br/>
<b>Notice</b>: Undefined index: HTTP_ACCEPT_LANGUAGE in <b>/srv/www/chroot/site05/web/app/bootstrap.php</b> on line <b>159</b><br/>
wrestlers_club</span> INFO1<br/>
<span class="text-danger font-weight-bold">wrestlers_birthday</span> INFO2<br/><br/>
<span class="text-danger font-weight-bold">wrestlers_licence_number</span> wrestlers_licence_no_licence<br/>
<span class="text-danger font-weight-bold">wrestlers_club_dl</span> wrestlers_licence_no_dl
</td>
我的代碼現在看起來:
info = soup.find('div', id='content')
club = info.findAll('span')
for clubs in club:
test = clubs.text
print(test)
結果是:
注意:
Undefined index: HTTP_ACCEPT_LANGUAGE in /srv/www/chroot/site05/web/app/bootstrap.php on line 159
wrestlers_club
wrestlers_birthday
wrestlers_licence_number
wrestlers_club_dl
如何提取 wreslters_club (INFO1)
和 wrestlers_birthday (INFO2)
背后的數據?
謝謝你的幫助!
使用以下Css selector
和find_next_sibling(text=True)
import requests
from bs4 import BeautifulSoup
res=requests.get("https://swisswrestling.ch/wrestlers?id=91")
soup=BeautifulSoup(res.text,'lxml')
print(soup.select_one('span.text-danger:nth-of-type(1)').find_next_sibling(text=True).strip())
print(soup.select_one('span.text-danger:nth-of-type(2)').find_next_sibling(text=True).strip())
輸出:
RC Willisau Lions
29 (1990)
這對你有用嗎? 我使用了您發布的鏈接,它提取了所需的信息。
import requests
import re
import lxml
import ssl
from bs4 import BeautifulSoup as bs
ssl._create_default_https_context = ssl._create_unverified_context
url = 'https://swisswrestling.ch/wrestlers?id=91'
strx = requests.get(url).text
regex = r"(wrestlers_club|wrestlers_birthday) (.*)\n"
soup = bs(strx, 'lxml')
for i in soup.find_all('td'):
print (*re.findall(regex,i.text), sep="\n")
輸出:
('wrestlers_club', 'RC Willisau Lions')
('wrestlers_birthday', '29 (1990)')
你可以試試這個,它給了我你要求的結果:
info = soup.find('div', id='content')
club = info.select('#content-element-94 > div.row.border.mx-0 > div > table > tbody > tr > td:nth-child(1)')
for clubs in club:
test = clubs.text
print(test)
輸出:
wrestlers_club RC Willisau Lions
wrestlers_birthday 29 (1990)
wrestlers_licence_number wrestlers_licence_no_licence
wrestlers_club_dl wrestlers_licence_no_dl
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.