python - BeautifulSoup 只抓取最后的结果

标签 python beautifulsoup

我正在尝试从此页面中抓取运行者姓名和提示数量:https://www.horseracing.net/racecards/newmarket/13-05-21

它仅返回决赛中最后一位参赛者的姓名。我已经一遍又一遍地这样做,但看不出我做错了什么。

有人能看到这个问题吗?

import requests
from requests import get
from bs4 import BeautifulSoup
import pandas as pd
import numpy as np

url = "https://www.horseracing.net/racecards/newmarket/13-05-21"

results = requests.get(url)

soup = BeautifulSoup(results.text, "html.parser")

date = []
course = []
time = []
runner = []
tips = []

runner_div = soup.find_all('div', class_='row-cell-right')

for container in runner_div:

    runner_name = container.h5.a.text
    runner.append(runner_name)

    tips_no = container.find('span', class_='tip-text number-tip').text if container.find('span', class_='tip-text number-tip') else ''
    tips.append(tips_no)


print(runner_name, tips_no)

最佳答案

尝试使用 print(runner, Tips) 而不是 print(runner_name, Tips_no):

输出:

print(runner, tips)
# ['Babindi', 'Turandot', 'Sharla', "Serena's Queen", 'Bellazada', 'Baby Alya', 'Adelita', 'Florence Street', 'Allerby', 'Puy Mary', 'Roman Mist', 'Lunar Shadow', 'Breakfastatiffanys', 'General Panic', 'Gidwa', 'Point Lynas', 'Three Dons', 'Wrought Iron', 'Desert Dreamer', 'Adatorio', 'Showmedemoney', 'The Charmer', 
# 'Bascinet', 'Dashing Rat', 'Appellation', 'Cambridgeshire', 'Danni California', 'Drifting Sands', 'Lunar Gold', 'Malathaat', 'Miss Calacatta', 'Sunrise Valley', 'Sweet Expectation', 'White Lady', 'Riknnah', 'Aaddeey', 'High Commissioner', 'Kaloor', 'Rodrigo Diaz', 'Mukha Magic', 'Gauntlet', 'Hawridge Flyer', 'Clovis Point', 'Franco Grasso', 'Kemari', 'Magical Land', 'Mobarhin', 'Movin Time', 'Night Of Dreams', 'Punta Arenas', 'Constanta', 'Cosmic George', 'Taravara', 'Basilicata', 'Top Brass', 'Without Revenge', 'Grand Scheme', 'Easy Equation', 'Mr Excellency', 'Colonel Faulkner', 'Urban War', 'Freak Out', 'Alabama Boy', 'Anghaam', 'Arqoob', 'Fiordland', 'Dickens', "Shuv H'Penny King"]
# ['5', '3', '1', '3', '1', '', '1', '', '', '', '1', '', '', '', '', '1', '', '', '12', '1', '', '', '', '', '', '', '5', '', '1', '', '', '7', '', '', '1', '11', '1', '', '', '', '', '2', '', '', '1', '3', '2', '9', '', '', '', '', '5', '1', '4', '', '5', '', '1', '4', '2', '1', '3', '2', '1', '', '', '']

关于python - BeautifulSoup 只抓取最后的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67517080/

相关文章:

python - 我如何访问YouTube第一搜索结果?

python - python中的字符串日期转换

python - 如何从拟合图像中提取点扩散函数?

python - 使用纽厄尔方法在 Python 中计算表面法线

python - Bs4 按类别查找 p 标签

python - 抓取股票数据并保存在csv中

python - python 单元测试中的信息性消息

python - 未能捕获第一个单词组

python - 如何使用 BeautifulSoup 查找一张表中的所有行

python - 使用 Beautfiul Soup 提取当前出价金额 - 动态更新值