猿问

Beautiful Soup 从 H2 字段中提取文本

我试图了解如何修复一些美丽的汤提取,


下面是我正在使用的示例代码,但现在它回来了


---> 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>


神不在的星期二
浏览 122回答 2
2回答

胡说叔叔

尝试这个:import requestsfrom bs4 import BeautifulSouppage = requests.get("https://new.tab.co.nz/extended-form/2020-09-18-m6-r1").textsoup = 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"]

温温酱

你可以试试这个:import requestsfrom bs4 import BeautifulSoup as bs# URL to be scrappedlink = "https://new.tab.co.nz/extended-form/2020-09-18-m6-r1"# Sending a get request to get the content of pagesource = requests.get(link).text# Parsing with help of bs4soup = bs(source,"html.parser")# Extracting the specific element from bs4 objectcontent = soup.find('h2', {'class': 'RaceHeader_title_1Yk'})# Getting the desired contentresult = content.text.split(" ")[-1]print(result)输出1600
随时随地看视频慕课网APP

相关分类

Python
我要回答