如何使用HTMLAgilityPack获取data-preconnect-urls

时间:2016-12-11 14:46:23

标签: c# asp.net parsing html-parsing html-agility-pack

我正在使用HTMLAgilityPack,而我正在尝试抓取 data-preconnect-urls 下的 http://www.hundsun.co.jp/ 链接。我怎么能得到它?

  <h3>
  <a style="display:none" href="/aclk?sa=L&amp;ai=DChcSEwimnPnc5OvQAhWRl70KHcxqCEAYABAA&amp;ei=9hZNWLqlCIyY8gXA04vACg&amp;sig=AOD64_3SZuXd57_-qOs8nnhn8rqw8GlIgw&amp;q=&amp;sqi=2&amp;ved=0ahUKEwi6-PTc5OvQAhUMjLwKHcDpAqgQ0QwIGA&amp;adurl=" id="s0p1c0"></a>
  <a href="https://www.google.co.jp/aclk?sa=L&amp;ai=DChcSEwimnPnc5OvQAhWRl70KHcxqCEAYABAA&amp;ei=9hZNWLqlCIyY8gXA04vACg&amp;sig=AOD64_3SZuXd57_-qOs8nnhn8rqw8GlIgw&amp;q=&amp;sqi=2&amp;ved=0ahUKEwi6-PTc5OvQAhUMjLwKHcDpAqgQ0QwIGA&amp;adurl=" id="vs0p1c0" onmousedown="return google.arwt(this)" data-preconnect-urls="http://www.hundsun.co.jp/" jsl="$t t-zxXzjt1d4B0;$x 0;" class="r-iA_xzYkgkx2Y">ブリッジSE募集中 - hundsun.co.jp‎</a>

1 个答案:

答案 0 :(得分:1)

你可以这样做:

//
// Play with finding substrings returning an array of the non-unique words and positions in text
//
//

import UIKit

let Bigstring = "Why is it so hard to find substrings in Swift3"
let searchStrs : Array<String>? = ["Why", "substrings", "Swift3"]

FindSubString(inputStr: Bigstring, subStrings: searchStrs)


func FindSubString(inputStr : String, subStrings: Array<String>?) ->    Array<(String, Int, Int)> {
    var resultArray : Array<(String, Int, Int)> = []
    for i: Int in 0...(subStrings?.count)!-1 {
        if inputStr.contains((subStrings?[i])!) {
            let range: Range<String.Index> = inputStr.range(of: subStrings![i])!
            let lPos = inputStr.distance(from: inputStr.startIndex, to: range.lowerBound)
            let uPos = inputStr.distance(from: inputStr.startIndex, to: range.upperBound)
            let element = ((subStrings?[i])! as String, lPos, uPos)
            resultArray.append(element)
        }
    }
    for words in resultArray {
        print(words)
    }
    return resultArray
}

你可以在这里试试:

https://dotnetfiddle.net/gMTFV3