Python - Beautiful Soup 查找文本不起作用

标签 python beautifulsoup

commentary = soup.find('div', {'id' : 'live-text-commentary-wrapper'})
findtoure = commentary.findAll(text = 'Gnegneri Toure Yaya')

我不明白为什么这不起作用。

评论的输出是:

<div id="live-text-commentary-wrapper">
  <h2 id="live-text-introduction">Live Text Commentary</h2>
  <div class="live-text blq-clearfix" id="live-text">
    <span>90:00 
    <span class="extra-info">+3:04 
    <span class="icon-live-text-full-time">Full time</span></span></span>
    <p class="event">
    <span class="event-title">
      <strong>Full Time</strong>
    </span> The referee ends the match.</p>
    <span>90:00 
    <span class="extra-info">+2:52</span></span>
    <p>Gael Clichy produces a cross, clearance made by Mike Williamson.</p>
    <span>90:00 
    <span class="extra-info">+0:41</span></span>
    <p>Shot by Shola Ameobi from 20 yards. Save made by Joe Hart.</p>
    <span>90:00 
    <span class="extra-info">+0:07</span></span>
    <p>The ball is crossed by Davide Santon, Demba Ba has an effort at goal. Joe Hart makes a save.</p>
    <span>88:26</span>
    <p>Assist by Gael Clichy.</p>
    <span>88:26 
    <span class="extra-info">
      <span class="icon-live-text-goal">Goal scored</span>
    </span></span>
    <p class="event">
    <span class="event-title">
    <strong>Goal</strong> - Gnegneri Toure Yaya - 
    <strong>Newcastle 0 - 2 Man City</strong></span> Gnegneri Toure Yaya grabs a goal from close in high into the middle of the
    goal. Newcastle 0-2 Man City.</p>
    <span>88:00</span>
    <p>Inswinging corner taken by Yohan Cabaye from the left by-line played to the near post, Vincent Kompany manages to make a
    clearance.</p>
    <span>87:45</span>
    <p>Shola Ameobi has a drilled shot. Micah Richards gets a block in.</p>
    <span>87:13</span>
    <p>The ball is swung over by Sergio Aguero, clearance made by Shola Ameobi.</p>
    <span>85:05 
    <span class="extra-info">
      <span class="icon-live-text-substitution">Substitution</span>
    </span></span>
    <p class="event">
    <span class="event-title">
      <strong>Substitution</strong>
    </span> (Man City) makes a substitution, with Micah Richards coming on for David Silva.</p>
    <span>84:24</span>
    <p>Shola Ameobi gives away a free kick for an unfair challenge on Joleon Lescott. Joe Hart restarts play with the free
    kick.</p>
    <span>83:43</span>
    <p>Inswinging corner taken left-footed by David Silva from the right by-line to the near post, Shot from close range by
    Edin Dzeko clears the bar.</p>
    <span>83:17</span>
    <p>Sergio Aguero takes a shot. Save by Tim Krul.</p>
    <span>81:44 
    <span class="extra-info">
      <span class="icon-live-text-substitution">Substitution</span>
    </span></span>
    <p class="event">
    <span class="event-title">
      <strong>Substitution</strong>
    </span> Cheick Tiote goes off and Ryan Taylor comes on.</p>
    <span>80:42</span>
    <p>Cheick Tiote gives away a free kick for an unfair challenge on Pablo Zabaleta. The ball is sent over by David Silva,
    Close range header by Gnegneri Toure Yaya goes wide of the left-hand upright.</p>
    <span>79:46</span>
    <p>Joe Hart takes the direct free kick.</p>
    <span>79:46 
    <span class="extra-info">
      <span class="icon-live-text-booking">Booking</span>
    </span></span>
    <p class="event">
    <span class="event-title">
      <strong>Booking</strong>
    </span> Shane Ferguson receives a yellow card for unsporting behaviour.</p>
    <span>79:38</span>
    <p>Shane Ferguson gives away a free kick for an unfair challenge on David Silva.</p>
    <span>78:58</span>
    <p>Free kick crossed by Yohan Cabaye, clearance by Gareth Barry.</p>
    <span>78:58 
    <span class="extra-info">
      <span class="icon-live-text-booking">Booking</span>
    </span></span>
    <p class="event">
    <span class="event-title">
      <strong>Booking</strong>
    </span> Pablo Zabaleta goes into the book for unsporting behaviour.</p>
  </div>
  <div class="live-text blq-clearfix" id="more-live-text">
    <span>78:50</span>
    <p>Free kick awarded for an unfair challenge on Shane Ferguson by Pablo Zabaleta.</p>
    <span>77:41</span>
    <p>The ball is crossed by Cheick Tiote, Edin Dzeko manages to make a clearance.</p>
    <span>74:24</span>
    <p>Shot by Sergio Aguero from deep inside the penalty area misses to the right of the target.</p>
    <span>76:38</span>
    <p>Corner taken by Yohan Cabaye from the left by-line to the near post, clearance made by Gareth Barry.</p>
    <span>76:19</span>
    <p>The ball is swung over by Yohan Cabaye, save by Joe Hart.</p>
    <span>75:48</span>
    <p>Centre by Pablo Zabaleta.</p>
    <span>74:26</span>
    <p>Shola Ameobi crosses the ball, Headed effort from inside the area by Papiss Cisse misses to the right of the goal.</p>
    <span>74:24</span>
    <p>Sergio Aguero produces a right-footed shot from deep inside the penalty area which goes wide of the right-hand post.</p>
    <span>73:52 
    <span class="extra-info">
      <span class="icon-live-text-substitution">Substitution</span>
    </span></span>
    <p class="event">
    <span class="event-title">
      <strong>Substitution</strong>
    </span> Shola Ameobi joins the action as a substitute, replacing Hatem Ben Arfa.</p>
    <span>73:32 
    <span class="extra-info">
      <span class="icon-live-text-substitution">Substitution</span>
    </span></span>
    <p class="event">
    <span class="event-title">
      <strong>Substitution</strong>
    </span> Shane Ferguson replaces James Perch.</p>
    <span>73:17</span>
    <p>Edin Dzeko has an effort at goal from deep inside the area missing to the left of the goal.</p>
    <span>72:36</span>
    <p>Gareth Barry fouled by Papiss Cisse, the ref awards a free kick. Direct free kick taken by Nigel De Jong.</p>
    <span>69:42</span>
    <p>Sergio Aguero provided the assist for the goal.</p>
    <span>69:42 
    <span class="extra-info">
      <span class="icon-live-text-goal">Goal scored</span>
    </span></span>
    <p class="event">
    <span class="event-title">
    <strong>Goal</strong> - Gnegneri Toure Yaya - 
    <strong>Newcastle 0 - 1 Man City</strong></span> Goal scored by Gnegneri Toure Yaya from just outside the penalty box to
    the bottom right corner of the goal. Newcastle 0-1 Man City.</p>
    <span>69:02 
    <span class="extra-info">
      <span class="icon-live-text-substitution">Substitution</span>
    </span></span>
    <p class="event">
    <span class="event-title">
      <strong>Substitution</strong>
    </span> Edin Dzeko joins the action as a substitute, replacing Carlos Tevez.</p>
    <span>66:49</span>
    <p>Demba Ba sends in a cross, Joleon Lescott manages to make a clearance.</p>
    <span>66:28</span>
    <p>Shot by Demba Ba from 18 yards. Clearance by Gareth Barry.</p>
    <span>65:23</span>
    <p>Centre by David Silva, Joleon Lescott challenges Tim Krul unfairly and gives away a free kick. Direct free kick taken by
    Tim Krul.</p>
    <span>65:01</span>
    <p>Inswinging corner taken left-footed by David Silva played to the near post, Papiss Cisse makes a clearance.</p>
    <span>64:54</span>
    <p>The ball is swung over by Carlos Tevez, clearance by Fabricio Coloccini.</p>
    <span>64:01</span>
    <p>Effort on goal by Hatem Ben Arfa from inside the penalty box misses to the right of the target.</p>
    <span>60:56 
    <span class="extra-info">
      <span class="icon-live-text-substitution">Substitution</span>
    </span></span>
    <p class="event">
    <span class="event-title">
      <strong>Substitution</strong>
    </span> Nigel De Jong replaces Samir Nasri.</p>
    <span>58:44</span>
    <p>David Silva has an effort at goal. Blocked by Mike Williamson.</p>
    <span>57:51</span>
    <p>The ball is crossed by James Perch, clearance by Joleon Lescott.</p>
    <span>57:05</span>
    <p>Foul by Jonas Gutierrez on Sergio Aguero, free kick awarded. Free kick taken by Gareth Barry.</p>
    <span>56:55</span>
    <p>Cheick Tiote crosses the ball, Samir Nasri makes a clearance.</p>
    <span>56:20</span>
    <p>Free kick awarded for an unfair challenge on Davide Santon by Gnegneri Toure Yaya. Free kick taken by Yohan Cabaye.</p>
    <span>54:36</span>
    <p>Shot from 20 yards from Carlos Tevez. Save made by Tim Krul.</p>
    <span>53:17</span>
    <p>Carlos Tevez produces a right-footed shot from the edge of the box and misses to the right of the target.</p>
    <span>53:01</span>
    <p>Centre by David Silva, Jonas Gutierrez makes a clearance.</p>
    <span>52:44</span>
    <p>Short corner taken by Samir Nasri.</p>
    <span>52:33</span>
    <p>Gael Clichy sends in a cross, blocked by James Perch.</p>
    <span>50:17</span>
    <p>Free kick awarded for an unfair challenge on Carlos Tevez by James Perch. Gnegneri Toure Yaya has an effort direct from
    the free kick.</p>
    <span>49:41</span>
    <p>Jonas Gutierrez sends in a cross, Pablo Zabaleta gets a block in.</p>
    <span>48:02</span>
    <p>Foul by Jonas Gutierrez on Pablo Zabaleta, free kick awarded. Joe Hart restarts play with the free kick.</p>
    <span>47:10</span>
    <p>The ball is swung over by Papiss Cisse, clearance made by Joleon Lescott.</p>
    <span>45:01</span>
    <p>The match restarts for the second half.</p>
    <span>45:00 
    <span class="extra-info">+1:09 
    <span class="icon-live-text-half-time">Half time</span></span></span>
    <p class="event">
    <span class="event-title">
      <strong>Half Time</strong>
    </span> The half-time whistle blows.</p>
    <span>45:00 
    <span class="extra-info">+0:07</span></span>
    <p>The referee blows for offside against Gareth Barry. Tim Krul takes the free kick.</p>
    <span>43:30</span>
    <p>Demba Ba gives away a free kick for an unfair challenge on Vincent Kompany. Free kick taken by Joe Hart.</p>
    <span>41:43</span>
    <p>Handball by Samir Nasri. Free kick taken by Tim Krul.</p>
    <span>41:32</span>
    <p>The ball is swung over by Sergio Aguero.</p>
    <span>40:35</span>
    <p>Gareth Barry has an effort at goal from 12 yards. Fabricio Coloccini gets a block in. Shot from 12 yards by Gareth
    Barry. Clearance made by Davide Santon.</p>
    <span>39:22</span>
    <p>A cross is delivered by Hatem Ben Arfa, Demba Ba produces a header from inside the area that goes over the bar.</p>
    <span>37:52</span>
    <p>Yohan Cabaye takes the free kick.</p>
    <span>37:52 
    <span class="extra-info">
      <span class="icon-live-text-booking">Booking</span>
    </span></span>
    <p class="event">
    <span class="event-title">
      <strong>Booking</strong>
    </span> Gnegneri Toure Yaya goes into the referee&#39;s book for unsporting behaviour.</p>
    <span>37:44</span>
    <p>Free kick awarded for an unfair challenge on Davide Santon by Gnegneri Toure Yaya.</p>
    <span>36:55</span>
    <p>The ball is swung over by Pablo Zabaleta.</p>
    <span>35:57</span>
    <p>Gnegneri Toure Yaya takes the free kick.</p>
    <span>35:57 
    <span class="extra-info">
      <span class="icon-live-text-booking">Booking</span>
    </span></span>
    <p class="event">
    <span class="event-title">
      <strong>Booking</strong>
    </span> The referee books James Perch for unsporting behaviour.</p>
    <span>35:38</span>
    <p>James Perch concedes a free kick for a foul on Carlos Tevez.</p>
    <span>34:26</span>
    <p>Yohan Cabaye produces a right-footed shot from just outside the box that goes wide left of the goal.</p>
    <span>33:42</span>
    <p>Shot from 12 yards from Demba Ba. Blocked by Gael Clichy. Shot by Hatem Ben Arfa from 12 yards. Save by Joe Hart.</p>
    <span>31:37</span>
    <p>David Silva crosses the ball in from the free kick.</p>
    <span>31:37 
    <span class="extra-info">
      <span class="icon-live-text-booking">Booking</span>
    </span></span>
    <p class="event">
    <span class="event-title">
      <strong>Booking</strong>
    </span> Cheick Tiote receives a caution for unsporting behaviour.</p>
    <span>31:25</span>
    <p>Free kick awarded for a foul by Cheick Tiote on David Silva.</p>
    <span>31:09</span>
    <p>Drilled right-footed shot by Demba Ba. Blocked by Joleon Lescott.</p>
    <span>30:40</span>
    <p>Samir Nasri has a drilled shot. Yohan Cabaye gets a block in.</p>
    <span>30:14</span>
    <p>Shot by Cheick Tiote. Vincent Kompany gets a block in.</p>
    <span>28:58</span>
    <p>Yohan Cabaye fires a strike on goal direct from the free kick, Carlos Tevez gets a block in.</p>
    <span>28:58 
    <span class="extra-info">
      <span class="icon-live-text-booking">Booking</span>
    </span></span>
    <p class="event">
    <span class="event-title">
      <strong>Booking</strong>
    </span> Gareth Barry goes into the referee&#39;s book.</p>
    <span>28:51</span>
    <p>Free kick awarded for a foul by Gareth Barry on Yohan Cabaye.</p>
    <span>28:15</span>
    <p>Demba Ba takes a shot from a long way out clearing the bar.</p>
    <span>25:39</span>
    <p>Shot comes in from Carlos Tevez from the free kick, Tim Krul makes a save.</p>
    <span>25:39 
    <span class="extra-info">
      <span class="icon-live-text-booking">Booking</span>
    </span></span>
    <p class="event">
    <span class="event-title">
      <strong>Booking</strong>
    </span> Hatem Ben Arfa is cautioned.</p>
    <span>25:29</span>
    <p>Hatem Ben Arfa gives away a free kick for an unfair challenge on Gael Clichy.</p>
    <span>22:48</span>
    <p>The offside flag is raised against Sergio Aguero. Yohan Cabaye restarts play with the free kick.</p>
    <span>22:05</span>
    <p>The ball is crossed by Gael Clichy.</p>
    <span>20:17</span>
    <p>Long distance shot from Gael Clichy misses to the left of the goal.</p>
    <span>19:44</span>
    <p>Corner taken left-footed by David Silva from the right by-line, James Perch manages to make a clearance.</p>
    <span>18:35</span>
    <p>The ball is sent over by Pablo Zabaleta, clearance by Mike Williamson.</p>
    <span>16:43</span>
    <p>Sergio Aguero takes a shot. Tim Krul makes a save.</p>
    <span>15:24</span>
    <p>Papiss Cisse is flagged offside by the assistant referee. Free kick taken by Vincent Kompany.</p>
    <span>10:15</span>
    <p>Shot by David Silva. Save made by Tim Krul.</p>
    <span>9:45</span>
    <p>Corner taken by Samir Nasri from the left by-line to the near post, clearance made by Cheick Tiote.</p>
    <span>9:18</span>
    <p>Samir Nasri takes a short corner, David Silva sends in a cross, blocked by Hatem Ben Arfa.</p>
    <span>8:54</span>
    <p>Samir Nasri produces a curled right-footed shot from 18 yards. Blocked by James Perch.</p>
    <span>6:31</span>
    <p>Unfair challenge on Gareth Barry by Yohan Cabaye results in a free kick. Gnegneri Toure Yaya takes the direct free
    kick.</p>
    <span>4:33</span>
    <p>Carlos Tevez is flagged offside by the assistant referee. Free kick taken by Tim Krul.</p>
    <span>1:15</span>
    <p>Free kick awarded for an unfair challenge on Hatem Ben Arfa by David Silva. Direct free kick taken by Yohan Cabaye.</p>
    <span>0:06</span>
    <p>Carlos Tevez concedes a free kick for a foul on Cheick Tiote. Yohan Cabaye takes the free kick.</p>
    <span>0:00</span>
    <p>The game kicks-off.</p>
  </div>
  <button class="hide blq-clearfix" id="show-more-live-text" style="display: none;" type="button">
    <span class="text-indicator">More Live Text</span>
  </button>
  <p class="disclaimer">Live text and data provided by The Press Association</p>
</div>

我最终想替换​​'Gnegneri Toure Yaya''Yaya Toure' .


编辑 - 我想将这些实例替换为 'Yaya Toure' 。但我随后希望能够像以前一样使用整个注释正文,但进行了更改(如果我使用 find 然后使用 .replace ,它将用 'Gnegneri Toure Yaya' 分隔这些位,但不会更改原始注释变量.

最佳答案

搜索类似的文本必须与标签中的整个文本匹配,在您的情况下就是- Gnegneri Toure Yaya -

将其改为正则表达式:

import re

findtoure = commentary.findAll(text = re.compile('Gnegneri Toure Yaya'))

这将匹配包含该名称的所有 NavigableString 元素。

关于Python - Beautiful Soup 查找文本不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15054574/

相关文章:

python - 无法获取用于屏幕抓取的 xpath

python - Python 中的 QDISC_BYPASS(原始套接字)

python - 找出游戏板上两个正方形之间的最短距离

python - 使用 Python 中的 BeautifulSoup 揭示网站上缺失的标签

python - BeautifulSoup 和 lxml 找不到 div 元素

python - 仅当有两个类并且共享相同的第一个时才选择元素

python - setuptools:使用源文件夹中的 setup.py 从 'project' 导入

python - 将 Pandas 数据框转换为 json 很慢

python - 如何根据 <p> <div> 或 <br> 标签的存在来分割 html 页面

Python 抓取空标签