2009-03-19 6 views
10

En essayant d'analyser html en utilisant Yahoo Query Language et xpath fonctionnalité fournie par YQL, j'ai rencontré des problèmes de ne pas être en mesure d'extraire "text()" ou des valeurs d'attribut.
Par exemple.
perma linkQuerying html en utilisant Yahoo YQL

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

donne une liste des points d'ancrage au format 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> 

Maintenant, lorsque je tente d'extraire la valeur de nœud à l'aide

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

j'obtenir des résultats concaténés plutôt qu'une liste des nœuds

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

Comment puis-je séparer en listes de noeuds et comment sélectionner des valeurs attribut?

Une requête comme celui-ci

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

m'a donné les mêmes résultats pour effectuer des requêtes div/h3/a

Répondre

20

YQL nécessite l'expression XPath pour évaluer un ItemPath plutôt que du texte du nœud. Mais une fois que vous avez un itemPath, vous pouvez projeter diverses valeurs depuis l'arbre

En d'autres termes, un ItemPath doit pointer vers le nœud dans le code HTML résultant plutôt que vers le contenu textuel/les attributs. YQL renvoie tous les nœuds correspondants et leurs enfants lorsque vous sélectionnez * dans les données.

exemple

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

Cela renvoie tout le correspondant XPath de l'a. Maintenant, pour projeter le contenu du texte, vous pouvez le projeter à l'aide de

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

"content" renvoie le contenu du texte contenu dans le nœud.

Pour projeter des attributs, vous pouvez le spécifier par rapport à l'expression xpath. Dans ce cas, puisque vous avez besoin du href qui est relatif à a.

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

ce retour <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>

Si vous avez besoin à la fois l'attribut 'href' et le textContent, vous pouvez exécuter la requête YQL suivante:

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

retours:

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

Espérons que cela aide. laissez-moi savoir si vous avez d'autres questions sur YQL.

+0

Fonctionne comme un charme! – Cherian

Questions connexes