无法使用jQuery提取html标记的文本

时间:2016-08-25 09:41:43

标签: jquery html web-scraping

我无法通过Chrome控制台提取html标记的内容。我想我错过了一些非常明显的东西,但我已经尝试了半个小时了,而且我无法让它发挥作用,所以我要求帮助:)

我想从该页面中提取一些代码:http://www.societe.com/societe/deezer-511716573.html

这是我感兴趣的html片段:

<table id="rensjur">
  <tbody>
    <tr>
     <td>Dénomination</td>
     <td>DEEZER</td>
    </tr>
  </tbody>
</table>

我想提取内容&#34; DEEZER&#34;第二个&lt; td&gt;。我使用以下内容:

$("#rensjur tbody td:nth-child(2)").text();

但这会引发错误:Uncaught TypeError: $(...).text is not a function(…)

然而,当我打电话时:

$("#rensjur tbody td:nth-child(2)")

它为我提供了正确的HTML标记:

<td>DEEZER</td>

我怎样才能得到&#34; DEEZER&#34;结果是 ?我对text()方法做错了什么?

4 个答案:

答案 0 :(得分:0)

我没有看到任何错误,它有效

console.log ( $("#myId tbody td:nth-child(2)").text() )
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="myId">
  <tbody>
    <tr>
     <td>Hello</td>
     <td>world</td>
    </tr>
  </tbody>
</table>

答案 1 :(得分:0)

使用innerHTML属性;这将有所帮助。

答案 2 :(得分:0)

&#13;
&#13;
alert($("#myId tbody td:nth-child(2)").text());
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<table id="myId">
  <tbody>
    <tr>
     <td>Hello</td>
     <td>world</td>
    </tr>
  </tbody>
</table>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

for item, value in inventory.items():
    print('{:12s} {:2d}'.format(item, value))
console.log ( $("#myId tbody td:nth-child(1)").innerHTML )