Skip to content

Instantly share code, notes, and snippets.

@nimzo6689
Created December 9, 2018 10:07
Show Gist options
  • Save nimzo6689/1731c7de8bc67c44e1fde06df30d27ea to your computer and use it in GitHub Desktop.
Save nimzo6689/1731c7de8bc67c44e1fde06df30d27ea to your computer and use it in GitHub Desktop.
日常で書いたXPath式をとにかく保存しまくるページ
Array.from($x('//*[@id="root"]/div/div/nav/ul/li/a[./h3[contains(text(), "conditional")]]/@href'))
.map(e => `${e.textContent}, title: ${e.ownerElement.textContent}`)
// ejje.weblio.jp/content/${word}
$x('//*[@class="EgtejRSpc"][2]')
// 品詞を取得する
$x('//*[@class="EgtejSub" and contains(text(), "詞")]')
// 品詞別に単語の解説を抽出したい場合
$x(`
//span[@class="EgtejSub" and contains(text(), "形容詞")]
/following-sibling::div[@class="EgtejWrp"]
`)
// 品詞別に例文を抽出したい場合
$x(`
//span[@class="EgtejSub" and contains(text(), "形容詞")]
/following-sibling::div[@class="EgtejWrp"]
/following-sibling::*[not(contains(@class, "EgtejYrAr"))]
`)
// ChomeやFireFoxのコンソールで実行する場合
Array.from($x('xpath')).map(e => e.textContent)
# サードパーティを使用しないと、標準ではHTMLに対してXPathができない。
Add-Type -Path $PsScriptRoot\HtmlAgilityPack.dll
$HAP= New-Object HtmlAgilityPack.HtmlDocument
$html= Invoke-RestMethod -Uri https://ejje.weblio.jp/content/jib
$HAP.LoadHtml($html)
$HAP.DocumentNode.SelectNodes('xpath')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment