我在尝试使用Kanna拉取图像URL时遇到问题。我正在尝试从此处获取“高质量数据”:

<div class="cui-content c-bdr-gray-clr ch-bdr-gray-md ">

  <div class="cui-udc-image-container">
    <div class="cui-udc-image-overlay"></div>
        <div class="cui-image-lazy-container cui-image-hover-zoom">
    <img class="cui-svg-placeholder c-bg-gray-bg" type="image/svg+xml" height="250"  width="414" src="data:image/svg+xml;charset=utf-8,%3Csvg xmlns%3D'http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg' height='250' width='414'%2F%3E" alt="image placeholder">

    <img class="cui-image  lazy-wl    progressive-load  "
    src="data:image/svg+xml;charset=utf-8,%3Csvg xmlns%3D'http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg' height='250' width='414'%2F%3E"
    data-original="//img.grouponcdn.com/deal/2LXY5HUANHJq7xXm3P5gNFJYBRB5/2L-1100x660/v1/c125x75q5.jpg"
    data-high-quality="//img.grouponcdn.com/deal/2LXY5HUANHJq7xXm3P5gNFJYBRB5/2L-1100x660/v1/c414x250q85.jpg"
    data-high-quality-2x="//img.grouponcdn.com/deal/2LXY5HUANHJq7xXm3P5gNFJYBRB5/2L-1100x660/v1/c414x250q85.jpg"
    alt="image for Up to 36% Off Las Vegas Margarita Mojito Festival">
  </div>

我只能从第一个“img”元素中获取信息,但我需要的信息包含在第二个元素中。我的代码如下所示:
func parseHtml(html: String) {
    var tempArray = [String]()

    if let doc = Kanna.HTML(html:html, encoding: String.Encoding.utf8) {

        for name in doc.css("div[class^='cui-image-lazy-container cui-image-hover-zoom']"){
            let imageUrl = (name.at_xpath("img")?["data-high-quality"])
            tempArray.append(imageUrl!)
        }
    }

我们欢迎所有的建议!

最佳答案

我想出了办法。我应该按类来获取实际的“img”节点,然后分别选择我要查找的HTML标记的哪一部分,如下所示:

for name in doc.css("img[class='cui-image  lazy-wl    progressive-load  ']"){

            tempArray.append(name["data-high-quality"]!)
        }

希望能对遇到相同问题的任何人有所帮助。

关于ios - 使用Kanna解析HTML,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49213842/

10-13 03:58