2009-03-19 21 views
10

Podczas próby parsowania html za pomocą Yahoo Query Language i funkcji xpath dostarczonej przez YQL, natknąłem się na problemy z nieodzyskaniem "text()" lub wartości atrybutów.
Dla np.
perma linkWypisywanie html przy użyciu Yahoo YQL

select * from html where url="http://stackoverflow.com" 
and xpath='//div/h3/a' 

podaje listę kotew jako XML

<results> 
    <a class="question-hyperlink" href="https://stackoverflow.com/questions/661184/filling-the-text-area-with-the-text-when-a-button-is-clicked" title="In ASP.net, I need the code to fill the text area (in the form) when a button is clicked. Can you help me through by showing a simple .aspx code containing the script tag? ">Filling the text area with the text when a button is clicked</a>... 
</results> 

Teraz, gdy próbuję wydobyć wartość węzła przy użyciu

select * from html where url="http://stackoverflow.com" 
and xpath='//div/h3/a/text()' 

dostaję wyniki łączone zamiast lista węzłów np

<results>Xcode: attaching to a remote process for debuggingWhy is b 
…… </results> 

Jak oddzielić go do listy węzłów i jak wybiorę atrybut wartości?

Zapytanie jak to

select * from html where url="http://stackoverflow.com" 
and xpath='//div/h3/a[@href]' 

dał mi takie same wyniki dla zapytań div/h3/a

Odpowiedz

20

YQL wymaga wyrażenia XPath do oceny do itemPath zamiast tekstu węzła. Ale gdy już masz obiekt itemPath, możesz wyświetlać różne wartości z drzewa:

Innymi słowy, ItemPath powinien wskazywać na Node w wynikowym HTML, a nie na treść tekstową/atrybuty. YQL zwraca wszystkie pasujące węzły i ich dzieci po wybraniu * z danych.

przykład

select * from html where url="http://stackoverflow.com" and xpath='//div/h3/a' 

ta zwraca wszystko A na dopasowanie XPath. Teraz, aby wyświetlać treść tekstową, możesz ją wyświetlać, używając "treści", która zwraca zawartość tekstową przechowywaną w węźle.

Aby wyświetlać atrybuty, można określić je względem wyrażenia xpath. W tym przypadku, ponieważ potrzebujesz href, który jest względem a.

select href from html where url="http://stackoverflow.com" and xpath='//div/h3/a' 

to zwraca <results> <a href="https://stackoverflow.com/questions/663973/putting-a-background-pictures-with-leds"/> <a href="https://stackoverflow.com/questions/663013/advantages-and-disadvantages-of-popular-high-level-languages"/> .... </results>

Jeśli potrzebne zarówno atrybut 'href' i textContent, można wykonać następujące YQL zapytanie:

select href, content from html where url="http://stackoverflow.com" and xpath='//div/h3/a' 

Powroty:

<results> <a href="https://stackoverflow.com/questions/663950/double-pointer-const-issue-issue">double pointer const issue issue</a>... </results> 

Nadzieję, że pomaga. daj mi znać, jeśli masz więcej pytań na temat YQL.

+0

Działa jak urok! – Cherian

Powiązane problemy