通过PyQuery拆分数据

时间:2015-01-21 14:26:21

标签: python web-scraping pyquery

我有以下情况:

<div class="entry">
<p>one</p>
<p>two<br />three<br />four</p>
<p>five<br />six</p>
</div>

我想屈服['one','two','three','four','five','six']

到目前为止,我有:

import PyQuery as pq
s = pq(html)
list = [i.text() for i in s('div.entry').find('p').items()]

这只会将其分解为<p>标记,完全忽略<br />标记。我尝试了以下内容:

list = [i.text() for i in s(table).find('p').find('br').items()]
list = [i.text() for i in s(table).find('p').find('br').prevAll().items()]
list = [i.split('\n') for i in s(table).find('br').replaceWith('\n')]

这些都没有奏效。此外,PyQuery API列出.replaceWith()作为有效函数,但是当我执行test = s(table).find('br').replaceWith('anytext')时,它不会替换任何内容而我没有错误,只是相同的项目列表它们之间有<br />个标签。 .replaceWith()以不同方式对待<br><br />吗?

更复杂的例子

<div class="entry">
<p>122 E. Washington St.<br />
734-665-8767</p>
<p>Amadeus is offering both pricing options.</p>
<p><strong>Lunch 2 for $15 </strong><br />
Choice of:<br />
<strong>Soup<br />
Green salad </strong></p>
<p>Choice of lunch dish:<br />
<strong>1 Golabek<br />
3 Piergies<br />
3 Placeki<br />
Kielbsa<br />
Kapusta salad<br />
Warsaw salad<br />
Artichoke salad<br />
Potato salad</strong></p>
<p><strong>Lunch $15</strong><br />
Three Course Meal<br />
Choice of lunch entrée with green salad and dessert</p>
<p><strong>Dinner 2 for $28</strong></p>
<p>Choice of:<br />
<strong>Cup of soup<br />
Green salad </strong></p>
<p>Choice of entrée:<br />
<strong>2 Potato Snitzel<br />
4 Potato Placeki<br />
6 Piergis<br />
2 Golabki<br />
Bigos<br />
Grilled Kielbsa<br />
Vegetarian combo<br />
Krakow Chicken </strong>(one breast)<br />
<strong>Tilapia<br />
Cold salad</strong></p>
<p><strong>Dinner $28</strong><br />
Four Course Meal<br />
Choice of soup + green salad + Dinner Entrée + Dessert </p>
<p><strong>Sunday Brunch $15</strong><br />

预期结果

[122 E. Washington St','734-665-8767','Amadeus is offering both pricing options.','Lunch 2 for $15','Choice of:','Soup','Green salad','Choice of lunch dish:','1 Golabek','3 Piergies','3 Placeki','Kielbsa',' Kapusta salad','Warsaw salad','Artichoke salad','Potato salad','Lunch $15','Three Course Meal','Choice of lunch entrée with green salad and dessert','Dinner 2 for $28','Choice of:','Cup of soup','Green salad','Choice of entrée:','2 Potato Snitzel','4 Potato Placeki','6 Piergis','2 Golabki','Bigos','Grilled Kielbsa','Vegetarian combo','Krakow Chicken (one breast)','Tilapia','Cold salad','Dinner $28','Four Course Meal','Choice of soup + green salad + Dinner Entrée + Dessert','Sunday Brunch $15']

1 个答案:

答案 0 :(得分:0)

看起来pyquery没有按预期运行。使用.contents()的解决方法:

>>> import lxml

>>> [e for ptag in s('div.entry').find('p').items()
       for e in ptag.contents()
       if isinstance(e, lxml.etree._ElementStringResult)]
['one', 'two ', 'three', 'four', 'five', 'six']