Python Belle Soupe analyser une table avec un ID spécifique
Je vais essayer d'obtenir les données à partir d'une table avec un ID spécifique qui, je le sais.
Pour quelque raison, le code continue à me donner Aucun résultat.
Du code HTML je suis en train de l'analyser:
<table cellspacing="0" cellpadding="3" border="0" id="ctl00_SPWebPartManager1_g_c001c0d9_0cb8_4b0f_b75a_7cc3b6f7d790_ctl00_HistoryData1_gridHistoryData_DataGrid1" style="width:100%;border-collapse:collapse;">
<tr class="gridHeader" valign="top">
<td class="titleGridRegNoB" align="center" valign="top"><span dir=RTL>שווי שוק (אלפי ש"ח)</span></td>
<td class="titleGridReg" align="center" valign="top">הון רשום למסחר</td>
<td class="titleGridReg" align="center" valign="top">שער נמוך</td><td class="titleGridReg" align="center" valign="top">שער גבוה</td>
<td class="titleGridReg" align="center" valign="top">שער בסיס</td>
<td class="titleGridReg" align="center" valign="top">שער פתיחה</td><td class="titleGridReg" align="center" valign="top"><span dir="rtl">שער נעילה (באגורות)</span></td>
<td class="titleGridReg" align="center" valign="top">שער נעילה מתואם</td><td class="titleGridReg" align="center" valign="top">תאריך</td>
</tr>
<tr onmouseover="this.style.backgroundColor='#FDF1D7'" onmouseout="this.style.backgroundColor='#ffffff'">
... Et ainsi de suite
Mon code:
html = br.response().read()
soup = BeautifulSoup(html)
table = soup.find(lambda tag: tag.name=='table' and tag.has_key('id') and tag['id']=="ctl00_SPWebPartManager1_g_c001c0d9_0cb8_4b0f_b75a_7cc3b6f7d790_ctl00_HistoryData1_gridHistoryData_DataGrid1")
rows = table.findAll(lambda tag: tag.name=='tr')
In [100]: print table
None
- Pourquoi ne pas simplement utiliser
find_all(tag, id="id_name")
? - Vous parlez de la création de lignes? Malheureusement, le tableau lui-même est vide, donc il n'a pas d'importance.. j'ai besoin d'obtenir le "tableau" de fait dès la première fois..
- C'est la même chose avec cette ligne
table = soup.find()
table = soup.find(tag, id="ctl00_SPWebPartManager1_g_c001c0d9_0cb8_4b0f_b75a_7cc3b6f7d790_ctl00_HistoryData1_gridHistoryData_DataGrid1")
donne:NameError: name 'tag' is not defined
- Cela fonctionne? J'ai ajouté une réponse.
Vous devez vous connecter pour publier un commentaire.
De l'amélioration aiKid réponse:
Puisque vous travaillez avec des données UTF-8, vous devez définir la chaîne comme une chaîne unicode comme
u"""(...)"""
. Tout ce que vous devez faire pour travailler avec unicode est ceci:Ci-dessus vous donnera une chaîne de caractères ASCII, que vous pourrez ensuite encoder en unicode. Comme, dire que la chaîne est stockée dans
html
, et vous pouvez l'encoder en arrière unicode à l'aide dehtml.encode("utf-8")
. Si vous faites cela, vous n'avez pas besoin de mettre leu
en face de quoi que ce soit. Vous pouvez traiter le tout comme une chaîne de nouveau.html = br.response().read()
?html = unicide(br.response(),read())
table_id = u"""ctl00_SPWebPartManager1_g_c001c0d9_0cb8_4b0f_b75a_7cc3b6f7d790_ctl00_HistoryData1_gridHistoryData_DataGrid1""" table = soup.findall(u"""table""", id=table_id)
Et j'obtiens:TypeError: 'NoneType' object is not callable
html = unicode(br.response(),read())
# encoding=utf-8
tout en haut du fichier?html = unicode(br.response().read())
retourneUnicodeDecodeError: 'ascii' codec can't decode byte 0xd7 in position 118: ordinal not in range(128)
TypeError: 'NoneType' object is not callable
pour la lignetable = soup.findall("table", id=table_id)
TypeError: 'NoneType' object is not callable