Вытащите href из тегов span - PullRequest
       0

Вытащите href из тегов span

1 голос
/ 08 марта 2020

Я не могу понять, как правильно извлекать href изнутри блока ниже, и в частности идентификаторы (hillge01, masonfr01).

<div>
  <strong>Inactive: </strong>
  <span><strong>MIL</strong></span>
  <a href="/players/h/hillge01.html">George Hill</a>,
  <a href="/players/m/masonfr01.html">Frank Mason</a>,
  <a href="/players/r/reynoca01.html">Cameron Reynolds</a>,
  <a href="/players/w/wilsodj01.html">D.J. Wilson</a>

  <span><strong>LAL</strong> </span>
  <a href="/players/a/antetko01.html">Kostas Antetokounmpo</a>,
  <a href="/players/c/cacokde01.html">Devontae Cacok</a>,
  <a href="/players/h/hortota01.html">Talen Horton-Tucker</a>,
  <a href="/players/w/waitedi01.html">Dion Waiters</a>
</div>

Пока мне удалось вытащить первый href, используя приведенный ниже код, но не удалось найти способ вернуть остаток href.

soup = get_soup(date_team)

for strong_tag in soup.findAll('strong'):
   if 'Inactive' in strong_tag.text:
   str1 = strong_tag.next_sibling.next_sibling
   print(str1)

Любая помощь по этому вопросу будет принята с благодарностью.

1 Ответ

0 голосов
/ 08 марта 2020

Попробуйте это. Решения с помощью SimplifiedDo c.

from simplified_scrapy import SimplifiedDoc
html = '''
<div>
  <strong>Inactive: </strong>
  <span><strong>MIL</strong> </span>
  <a href="/players/h/hillge01.html">George Hill</a>,
  <a href="/players/m/masonfr01.html">Frank Mason</a>, 
  <a href="/players/r/reynoca01.html">Cameron Reynolds</a>, 
  <a href="/players/w/wilsodj01.html">D.J. Wilson</a> 
  <span><strong>LAL</strong> </span>
  <a href="/players/a/antetko01.html">Kostas Antetokounmpo</a>, 
  <a href="/players/c/cacokde01.html">Devontae Cacok</a>,
  <a href="/players/h/hortota01.html">Talen Horton-Tucker</a>, 
  <a href="/players/w/waitedi01.html">Dion Waiters</a>
</div>
'''
doc = SimplifiedDoc(html)
strong = doc.getElementByText('Inactive',tag='strong')
next = strong.getNext('a')
print(next)
next = next.next
print(next)

Результат:

{'href': '/players/h/hillge01.html', 'tag': 'a', 'html': 'George Hill'}
{'href': '/players/m/masonfr01.html', 'tag': 'a', 'html': 'Frank Mason'}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...