2015-07-15 5 views
-5

мне нужна помощь с BeautifulSoup, я пытаюсь получить данные:Python: Получение данных BeautifulSoup

<font face="arial" font-size="16px" color="navy">001970000521</font>

Их много, и мне нужно получить значение внутри «шрифт»

<div id="accounts" class="elementoOculto"> 
      <table align="center" border="0" cellspacing=0 width="90%">   <tr><th align="left" colspan=2>     permisos   </th></tr><tr> 
    <td colspan=2> 
     <table width=100% align=center border=0 cellspacing=1> 
      <tr> 
       <th align=center width="20%">cuen</th> 
       <th align=center>Mods</th> 
      </tr> 
     </table> 
    </td> 
</tr> 
</table> 
<table align="center" border="0" cellspacing=1 width="90%"> 
    <tr bgcolor="whitesmoke" height="08"> 
     <td align="left" width="20%"> 
      <font face="arial" font-size="16px" color="navy">001970000521</font> 
     </td> 
     <td>...... 

<table align="center" border="0" cellspacing=1 width="90%"> 
     <tr bgcolor="whitesmoke" height="08"> 
      <td align="left" width="20%"> 
       <font face="arial" font-size="16px" color="navy">001970000521</font> 
      </td> 

Надеюсь, вы можете мне помочь, спасибо.

+2

Можете ли вы документировать, что вы пробовали? –

ответ

1

Вы должны использовать метод bs4.Tag.find_all или что-то подобное.

soup.find_all(attrs={"face":"arial","font-size":"16px","color":"navy"}) 

Пример:

>>>import bs4 
>>>html='''<div id="accounts" class="elementoOculto">    <table align="center" border="0" cellspacing=0 width="90%">   <tr><th align="left" colspan=2>     permisos   </th></tr><tr>  <td colspan=2>   <table width=100% align=center border=0 cellspacing=1>    <tr>     <th align=center width="20%">cuen</th>     <th align=center>Mods</th>    </tr>   </table>  </td> </tr> </table> <table align="center" border="0" cellspacing=1 width="90%">  <tr bgcolor="whitesmoke" height="08">   <td align="left" width="20%">    <font face="arial" font-size="16px" color="navy">001970000521</font>   </td>   <td>...... <table align="center" border="0" cellspacing=1 width="90%">   <tr bgcolor="whitesmoke" height="08">    <td align="left" width="20%">     <font face="arial" font-size="16px" color="navy">001970000521</font>    </td> ''' 
>>>print bs4.BeautifulSoup(html).find_all(attrs={"face":"arial","font-size":"16px","color":"navy"}) 
[<font color="navy" face="arial" font-size="16px">001970000521</font>, <font color="navy" face="arial" font-size="16px">001970000521</font>] 
+0

i get 'NoneType' объект не имеет атрибута 'string' – jcrashvzla

0

Как об использовании CSS selector начиная с div с id="accounts":

soup.select("div#accounts table > tr > font") 
1

как об этом?

from bs4 import BeautifulSoup 
str = '''<div id="accounts" class="elementoOculto"> 
      <table align="center" border="0" cellspacing=0 width="90%">   <tr><th align="left" colspan=2>     permisos   </th></tr><tr> 
    <td colspan=2> 
     <table width=100% align=center border=0 cellspacing=1> 
      <tr> 
       <th align=center width="20%">cuen</th> 
       <th align=center>Mods</th> 
      </tr> 
     </table> 
    </td> 
</tr> 
</table> 
<table align="center" border="0" cellspacing=1 width="90%"> 
    <tr bgcolor="whitesmoke" height="08"> 
     <td align="left" width="20%"> 
      <font face="arial" font-size="16px" color="navy">001970000521</font> 
     </td> 
     <td>...... 

<table align="center" border="0" cellspacing=1 width="90%"> 
     <tr bgcolor="whitesmoke" height="08"> 
      <td align="left" width="20%"> 
       <font face="arial" font-size="16px" color="navy">001970000521</font> 
      </td>''' 

bs = BeautifulSoup(str) 
print bs.font.string 
+0

i get 'NoneType' объект не имеет атрибута 'string' – jcrashvzla

+0

Какую версию python вы использовали? – zveryansky

+0

Извините, ошибка возникает, когда я получаю суп, «bs = BeautifulSoup (str)» в html только я держу кусок кода и заканчивая, прежде чем вы начнете часть кода, который я ищу. Как я могу сделать, чтобы другой начал тег html? – jcrashvzla

Смежные вопросы