为了账号安全,请及时绑定邮箱和手机立即绑定

Beautiful Soup 从 H2 字段中提取文本

Beautiful Soup 从 H2 字段中提取文本

神不在的星期二 2023-08-15 16:16:44
我试图了解如何修复一些美丽的汤提取,下面是我正在使用的示例代码,但现在它回来了---> 66             dista = soup.find('h2', {'class': 'RaceHeader_title_1Yk'}).text     67             dista = dista.split( " " )[-1]     68             horses = soup.findAll('div', {'class': 'Entries_entry_2Xt'})AttributeError: 'NoneType' object has no attribute 'text 下面是我正在使用的代码以及它正在抓取的内容的示例,理想情况下我试图获取“1600”作为输出 dista = soup.find('h2', {'class': 'RaceHeader_title_1Yk'}).text dista = dista.split( " " )[-1]<h2 class="RaceHeader_title_1Yk"><span class="RaceHeader_titleNumber_uNI">R1</span>"MT SOMERS HONEY MAIDEN 1600""1600"</h2>
查看完整描述

2 回答

?
胡说叔叔

TA贡献1804条经验 获得超8个赞

尝试这个:


import requests

from bs4 import BeautifulSoup


page = requests.get("https://new.tab.co.nz/extended-form/2020-09-18-m6-r1").text

soup = BeautifulSoup(page, "html.parser")

print(soup.find("h2", {"class": "RaceHeader_title_1Yk"}).text.split()[-1])

输出1600


要获取所有马,请添加以下行:


print([h.text for h in soup.find_all("span", {"class": "EntryHeader_runner_UwW"})])

输出:


['Danny Green (8) 5 g bay', 'Eisenhower (10) 5 g bay', 'On The Rivet (13) 4 g bay', 'Point Break (11) 4 g brown', 'Magie Noire (7) 4 g bay', 'Mazzoni (12) 7 g bay', 'Miss Oaks (3) 5 m bay', 'Turn Your Eyes (6) 5 m chestnut', 'Repulse (5) 4 m bay', 'Spindleshanks (9) 5 m bay', 'Nifty (1) 6 m chestnut', 'Tennessee Rock (14) 4 m bay', 'Wendy Darling (4) 4 m brown', "Tappy's Lad (2) 3 g brown"]



查看完整回答
反对 回复 2023-08-15
?
温温酱

TA贡献1752条经验 获得超4个赞

你可以试试这个:


import requests

from bs4 import BeautifulSoup as bs


# URL to be scrapped

link = "https://new.tab.co.nz/extended-form/2020-09-18-m6-r1"


# Sending a get request to get the content of page

source = requests.get(link).text


# Parsing with help of bs4

soup = bs(source,"html.parser")


# Extracting the specific element from bs4 object

content = soup.find('h2', {'class': 'RaceHeader_title_1Yk'})


# Getting the desired content

result = content.text.split(" ")[-1]


print(result)

输出


1600


查看完整回答
反对 回复 2023-08-15
  • 2 回答
  • 0 关注
  • 121 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信