【问题标题】:Beautiful Soup extracting text from a H2 fieldBeautiful Soup 从 H2 字段中提取文本
【发布时间】:2020-09-17 13:14:46
【问题描述】:

我想了解如何解决一些美丽的汤提取问题,

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

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

【问题讨论】:

标签: python beautifulsoup


【解决方案1】:

试试这个:

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

【讨论】:

    【解决方案2】:

    你可以试试这个:

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-28
      • 2021-04-20
      • 2021-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多