我想从transfermarkt.de上检索几个玩家的信息,比如曼努埃尔·诺伊尔生日。下面是相关的html的样子:
<tr>
<th>Geburtsdatum:</th>
<td>
<a href="/aktuell/waspassiertheute/aktuell/new/datum/1986-03-27">27.03.1986</a>
</td>
</tr>我知道我可以通过使用以下代码获得日期:
soup = BeautifulSoup(source_code, "html.parser")
player_attributes = soup.find("table", class_ = 'auflistung')
rows = player_attributes.find_all('tr')
date_of_birth = re.search(r'([0-9]+\.[0-9]+\.[0-9]+)', rows[1].get_text(), re.M)[0]但这是相当脆弱的。对于罗伯特·莱万多夫斯基来说,出生日期处于不同的位置。因此,出现在球员配置文件中的属性是不同的。有什么方法能在逻辑上做到
越健壮越好:)
发布于 2020-11-25 21:06:33
BeautifulSoup允许使用findNext()方法检索下一个兄弟姐妹。
from bs4 import BeautifulSoup
import requests
html = requests.get('https://www.transfermarkt.de/manuel-neuer/profil/spieler/17259', headers = {'User-Agent': 'Custom'})
soup = BeautifulSoup(source_code, "html.parser")
player_attributes = soup.find("table", class_ = 'auflistung')
rows = player_attributes.find_all('tr')
def get_table_value(rows, table_header):
for row in rows:
helpers = row.find_all(text=re.compile(table_header))
if helpers is not None:
for helper in helpers:
return helper.find_next('td').get_text()https://stackoverflow.com/questions/65009727
复制相似问题