thp / urlwatch

Watch (parts of) webpages and get notified when something changes via e-mail, on your phone or via other means. Highly configurable.
https://thp.io/2008/urlwatch/
Other
2.81k stars 352 forks source link

XPath/CSS filter: `exclude` not working properly #749

Closed kongomongo closed 1 year ago

kongomongo commented 1 year ago

Hi there,

many thanks for this wonderful tool. I have now come up various times to the following problem:

I'm scraping data as HTML and there are certain things that I want to exclude in the comparison. Mostly it works via by-tag or css or even xpath to select only the relevant portions.

But sometimes it is much harder to select the relevant content (because of fuzzy, generated or changing classes/ids) that to exclude the irrelevant content.

I have two examples. One is a "page footer" that is a

thp commented 1 year ago

If it's something that can be done line-based, grepi is the filter you might want, using regular expressions.

kongomongo commented 1 year ago

Unfortunately it is impossible using grep :(

thp commented 1 year ago

Then I found out about css: selector: "*" exclude: ".ads". But this seems to somehow clone the output multiple times and makes the output unusable.

This exclude subfilter should actually be working. Do you have a minimal example (input / filter / expected output / actual output) that we can use to investigate the issue?

thp commented 1 year ago

There's even an example in the docs:

Another useful option with XPath and CSS filters is ``exclude``.
Elements selected by this ``exclude`` expression are removed from the
final result. For example, the following job will not have any ``<a>``
tag in its results:

.. code:: yaml

   url: https://example.org/css-exclude.html 
   filter:
     - css:
         selector: body
         exclude: a

And for that one, we even have testcase data:

https://example.org/css-exclude.html:
  input: |-
    <html>
    <body>
      <h1>A page in a book</h1>
      <p>And some paragraph, too. <a href="http://example.net/">Also check out example.net!</a></p>
    </body>
    </html>
  output: |
    <body>
      <h1>A page in a book</h1>
      <p>And some paragraph, too. </p>
    </body>

I guess it might be that selector: "*" is the problem, and using body or html as the selector might be better?

kongomongo commented 1 year ago

Thanks for the input !

Yes, replacing * with body did the trick! Now nothing gets cloned or otherwise mangled. Awesome.

Just for reference. These were my inputs:

stuff.html:

    <div class="thumbnail" tr="Produktliste">
        <a href="https://some-site.info/BRAND-Special-modern-type-Product-long-Muster-weiss" name="1263/24/91/40"><img src="/$WS/sitesite/websale8_shop-sitesite/produkte/medien/bilder/klein/OL6WAHM1_M0_1.jpg" title="BRAND Special modern fit Product long Muster wei" alt="BRAND Special modern fit Product long Muster wei" class="artikel"></a>
        <div class="nachhaltigkeit-stoerer"></div>
    </div>
    <div class="product_list_div_content" >
            <a href="https://some-site.info/BRAND-Special-modern-type-Product-long-Muster-weiss" class="produktLink pr-name"><span>BRAND Special modern fit Product long Muster wei</span></a>
        <a href="https://some-site.info/BRAND-Special-modern-type-Product-long-Muster-weiss" class="produktLink discount_ja sale-stoerer BRAND-stoerer">%</a>
 <div class="preisinfo">
  <div class='produktbewertung bewertung_OL6WAHM1M0'></div>
                                                <p class="uvpprice">UVP <span class="uvp">69.95 &euro;</span></p>
                                        <p class="price price_sale">54,99 &euro;</p>
        </div>
        <div class="GroesseVisibleOnHover">              
                    <div class="weitere_farben">
                    <p>Weitere Farben:</p>
                        <div class="weitere_farbe">
                        <a href="https://some-site.info/BRAND-Special-modern-type-Product-long-Muster-navy" itemprop="isSimilarTo"><img src="/$WS/sitesite/websale8_shop-sitesite/produkte/medien/bilder/klein/OL6WAHM1_M6_1.jpg" alt="BRAND Special modern fit Product long Muster navy preisreduziert" title="BRAND Special modern fit Product long Muster navy preisreduziert"></a>
                        </div>
                    </div>
        </div>
        </div>
    <br class="clear">
</div>
command: "cat ~/stuff.html"
filter:
  - css:
      selector: "*"
      exclude: ".weitere_farben,div.thumbnail > a"
  - html2text:
      method: lynx
      nolist:
  - strip

WRONG output using *

BRAND Special modern fit Product long Muster wei %

   UVP 69.95 €

   54,99 €

   BRAND Special modern fit Product long Muster wei %

   UVP 69.95 €

   54,99 €

   BRAND Special modern fit Product long Muster wei %

   UVP 69.95 €

   54,99 €

   BRAND Special modern fit Product long Muster wei %

   UVP 69.95 €

   54,99 €

   BRAND Special modern fit Product long Muster wei BRAND
   Special modern fit Product long Muster wei %

   UVP 69.95 €

   54,99 €

   UVP 69.95 €
   69.95 €

   54,99 €

correct output using body even though theres no body tag in the input ;-)

BRAND Special modern fit Product long Muster wei %

   UVP 69.95 €

   54,99 €
thp commented 1 year ago

Closing this as "works for me" based on your comment.