BeautifulSoup: Как извлечь текст, заключенный в несколько тегов div / span / id - PullRequest
0 голосов
/ 09 февраля 2019

Мне нужно извлечь цифры (0,04) в тег "td" в конце этой HTML-страницы.

      <div class="boxContentInner">
         <table class="values non-zebra">
   <thead>
   <tr>
      <th>Apertura</th>
      <th>Max</th>
      <th>Min</th>
      <th>Variazione giornaliera</th>
      <th class="last">Variazione %</th>
   </tr>
   </thead>
   <tbody>
   <tr>
      <td id="open" class="quaternary-header">2708.46</td>
      <td id="high" class="quaternary-header">2710.20</td>
      <td id="low" class="quaternary-header">2705.66</td>
      <td id="change" class="quaternary-header changeUp">0.99</td>
      <td id="percentageChange" class="quaternary-header last changeUp">0.04</td>
   </tr>
   </tbody>
</table>

      </div>

Я пробовал этот код, используя BeautifulSoup с Python 2.8:


from bs4 import BeautifulSoup 
import requests 

page= requests.get('https://www.ig.com/au/indices/markets-indices/us-spx-500').text 
soup = BeautifulSoup(page, 'lxml') 

percent= soup.find('td',{'id':'percentageChange'}) 
percent2=percent.text


print percent2


Результат НЕТ.

Где ошибка?

1 Ответ

0 голосов
/ 09 февраля 2019

Я посмотрел на https://www.ig.com/au/indices/markets-indices/us-spx-500, и кажется, что вы не ищете правильный идентификатор при выполнении percent= soup.find('td', {'id':'percentageChange'})

Фактическое значение находится в <span data-field="CPC">VALUE</span>

enter image description here

Вы можете получить эту информацию с помощью следующего:

percent = soup.find("span", {'data-field': 'CPC'})
print(percent.text.strip())
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...