gawel / pyquery

A jquery-like library for python
http://pyquery.rtfd.org/
Other
2.29k stars 182 forks source link

when having more than one items, html() return wrong elements? or I miss understand. #191

Closed sunxk closed 3 years ago

sunxk commented 6 years ago

the code below:

import pyquery
from pyquery import PyQuery as pq

html="""
<body>
<div class="zztj">
<ul>
<li><a href="h_54282438000.html">cancer related</a></li>
<li><a href="h_53694605000.html">cancer origin</a></li>
<li><a href="h_50004173000.html">nasopharyngeal cancer</a></li>
<li><a href="h_50004870000.html">solid cancer</a></li>
<li><a href="h_50005806000.html">cancer antigen</a></li>
<li><a href="h_50871959000.html">transferred cancer</a></li>
<li><a href="h_52154903000.html">cancer protein</a></li>
<li><a href="h_54280474000.html">thoracic cancer</a></li>
<li><a href="h_50009705000.html">esophageal cancer</a></li>
<li><a href="h_51282611000.html">cancer line</a></li>
<li><a href="h_50011805000.html">cancer tissue</a></li>
<li><a href="h_50012720000.html">gastric cancer</a></li>
</ul></div>
</body>
"""

doc = pq(html)
print(doc(".zztj > ul > li").html())

the outputs:

<a href="h_54282438000.html">cancer related</a>
liquancss commented 3 years ago

In jQuery, $(".zztj > ul > li").html() alse return the first element. See here https://api.jquery.com/html/ 8KZSRWS``MQIn jQuery, $(".zztj > ul > li").html() alse return the first element. See here [https://api.jquery.com/html/](url)
RFI M{G@V0

if you want to get all the items. Try this.

doc = pq(html)
print(list(doc(".zztj > ul > li").map(lambda  i, ele: doc(ele).html())))