I'm trying to use pyparsing write a screenscraper. I've got some
arbitrary HTML text I define as opener & closer. In between is the HTML
data I want to extract. However, the data may contain the same
characters as used in the closer (but not the exact same text,
obviously). I'd like to get the *minimal* amount of data between these.
Here's an example (whitespace may differ):
from pyparsing import *
test=r"""<tr class="tableTop Space"><td></td></tr>
<tr class="tableTit leDark"><td class="tableTit leDark">Job
Information</td></tr><tr><td><tab le width="100%" border="0"
cellspacing="3" ><tr>
<td width="110" valign="top"><d iv align="right">< strong>Job Title:
</strong></div></td>
<td class="ccDispla yCell">Big Old <B
STYLE="backgrou nd-color:#FFEF95"> Head Honcho</B> Boss Man</td></tr>
<tr>
<td width="110" valign="top"><d iv align="right">< strong>Employer :
</strong></div></td>
<td width="200" nowrap class="ccDispla yCell"><table>< tr><td colspan="2"
valign="top">Gl obal Megacorp</td></tr></table></td><td>
<script>
function escapecomp(){
}
"""
data=Combine(On eOrMore(Word(pr intables)), adjacent=False,
joinString=" ")
title_open=Lite ral(r"""<td width="110" valign="top"><d iv
align="right">< strong>Job Title: </strong></div></td>
<td class="ccDispla yCell">""")
title_open.supp ress()
title_close=Lit eral(r"""</td>""")
title_close.sup press()
title=title_ope n + data + title_close
title2=title_op en + (data | title_close)
[color=blue][color=green][color=darkred]
>>> title.scanStrin g(test).next()[/color][/color][/color]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
StopIteration
[color=blue][color=green][color=darkred]
>>> title2.scanStri ng(test).next()[/color][/color][/color]
((['<td width="110" valign="top"><d iv align="right">< strong>Job Title:\n
</strong></div></td>\n<td class="ccDispla yCell">', 'Big Old <B
STYLE="backgrou nd-color:#FFEF95"> Head Honcho</B> Boss Man</td> </tr>
<tr> <td width="110" valign="top"><d iv align="right">< strong>Employer :
</strong></div></td> <td width="200" nowrap
class="ccDispla yCell"><table>< tr><td colspan="2" valign="top">Gl obal
Megacorp</td></tr></table></td> <td> <script> function escapecomp(){
}'], {}), 182, 656)[color=blue][color=green][color=darkred]
>>>[/color][/color][/color]
I'd expected title to work, but it doesn't match at all. ;( In other
test variants, title2 gives extra stuff at the end though not
necessarily to the end of the string (due to unprintable characters,
perhaps).
I want a ParseResult more like:
['<td width="110" valign="top"><d iv align="right">< strong>Job Title:\n
</strong></div></td>\n<td class="ccDispla yCell">', 'Big Old <B
STYLE="backgrou nd-color:#FFEF95"> Head Honcho</B> Boss Man, '</td>']
I sort of understand why title2 works as it does (the OneOrMore just
slurps up everything), but for the life of me I can't figure out how to
fix it. ;) Is there a way of writing something similar to RE's ".*?" ?
--Pete
--
Peter Fein pfein@pobox.com 773-575-0694
Basically, if you're not a utopianist, you're a schmuck. -J. Feldman
arbitrary HTML text I define as opener & closer. In between is the HTML
data I want to extract. However, the data may contain the same
characters as used in the closer (but not the exact same text,
obviously). I'd like to get the *minimal* amount of data between these.
Here's an example (whitespace may differ):
from pyparsing import *
test=r"""<tr class="tableTop Space"><td></td></tr>
<tr class="tableTit leDark"><td class="tableTit leDark">Job
Information</td></tr><tr><td><tab le width="100%" border="0"
cellspacing="3" ><tr>
<td width="110" valign="top"><d iv align="right">< strong>Job Title:
</strong></div></td>
<td class="ccDispla yCell">Big Old <B
STYLE="backgrou nd-color:#FFEF95"> Head Honcho</B> Boss Man</td></tr>
<tr>
<td width="110" valign="top"><d iv align="right">< strong>Employer :
</strong></div></td>
<td width="200" nowrap class="ccDispla yCell"><table>< tr><td colspan="2"
valign="top">Gl obal Megacorp</td></tr></table></td><td>
<script>
function escapecomp(){
}
"""
data=Combine(On eOrMore(Word(pr intables)), adjacent=False,
joinString=" ")
title_open=Lite ral(r"""<td width="110" valign="top"><d iv
align="right">< strong>Job Title: </strong></div></td>
<td class="ccDispla yCell">""")
title_open.supp ress()
title_close=Lit eral(r"""</td>""")
title_close.sup press()
title=title_ope n + data + title_close
title2=title_op en + (data | title_close)
[color=blue][color=green][color=darkred]
>>> title.scanStrin g(test).next()[/color][/color][/color]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
StopIteration
[color=blue][color=green][color=darkred]
>>> title2.scanStri ng(test).next()[/color][/color][/color]
((['<td width="110" valign="top"><d iv align="right">< strong>Job Title:\n
</strong></div></td>\n<td class="ccDispla yCell">', 'Big Old <B
STYLE="backgrou nd-color:#FFEF95"> Head Honcho</B> Boss Man</td> </tr>
<tr> <td width="110" valign="top"><d iv align="right">< strong>Employer :
</strong></div></td> <td width="200" nowrap
class="ccDispla yCell"><table>< tr><td colspan="2" valign="top">Gl obal
Megacorp</td></tr></table></td> <td> <script> function escapecomp(){
}'], {}), 182, 656)[color=blue][color=green][color=darkred]
>>>[/color][/color][/color]
I'd expected title to work, but it doesn't match at all. ;( In other
test variants, title2 gives extra stuff at the end though not
necessarily to the end of the string (due to unprintable characters,
perhaps).
I want a ParseResult more like:
['<td width="110" valign="top"><d iv align="right">< strong>Job Title:\n
</strong></div></td>\n<td class="ccDispla yCell">', 'Big Old <B
STYLE="backgrou nd-color:#FFEF95"> Head Honcho</B> Boss Man, '</td>']
I sort of understand why title2 works as it does (the OneOrMore just
slurps up everything), but for the life of me I can't figure out how to
fix it. ;) Is there a way of writing something similar to RE's ".*?" ?
--Pete
--
Peter Fein pfein@pobox.com 773-575-0694
Basically, if you're not a utopianist, you're a schmuck. -J. Feldman
Comment