10

Yahoo Query Language と YQL が提供する xpath 機能を使用して html を解析しようとすると、「text()」または属性値を抽出できないという問題に遭遇しました。パーマリンク
など

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

アンカーのリストを xml として与える

<results>
    <a class="question-hyperlink" href="/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> 

を使用してノード値を抽出しようとすると

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

ノードリストではなく連結された結果を取得します。

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

それをノード リストに分割する方法と、属性値を選択する方法を教えてください。

このようなクエリ

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

クエリで同じ結果が得られましたdiv/h3/a

4

1 に答える 1

20

YQLでは、ノードテキストではなくitemPathに評価するためにxpath式が必要です。ただし、itemPathを取得すると、ツリーからさまざまな値を投影できます。

つまり、ItemPathは、テキストのコンテンツ/属性ではなく、結果のHTMLのノードを指す必要があります。データから*を選択すると、YQLは一致するすべてのノードとその子を返します。

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

これにより、xpathに一致するすべてのaが返されます。これで、テキストコンテンツを投影するために、を使用して投影できます。

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

「content」は、ノード内に保持されているテキストコンテンツを返します。

属性を投影するために、xpath式を基準にして属性を指定できます。この場合、に関連するhrefが必要なので。

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

これは戻ります <results> <a href="/questions/663973/putting-a-background-pictures-with-leds"/> <a href="/questions/663013/advantages-and-disadvantages-of-popular-high-level-languages"/> .... </results>

属性'href'とtextContentの両方が必要な場合は、次のYQLクエリを実行できます。

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

戻り値:

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

お役に立てば幸いです。YQLについて他にご不明な点がありましたらお知らせください。

于 2009-03-19T20:48:45.490 に答える