首页 > 解决方案 > Swift 如何从实时网站中抓取前 500 或 1000 个顶级网站?

问题描述

首先,我不知道从https://gtmetrix.com/top1000.html抓取数据是否合适,但我想为我的应用获取前 500 或 1000 个网站的列表。因此,当用户搜索某个网站时,该应用程序可以提供建议的结果。

我正在尝试通过以下代码获取本网站的 html 内容

        let url = URL(string:"https://gtmetrix.com/top1000.html")
    do{
         let html = try String(contentsOf: url!, encoding: String.Encoding.ascii)
        print(html)
    }catch {
        print("woops")
    }

但是html字符串似乎不一致,并且不包含我想要的信息,例如标签tbody和/tbody之间缺少的部分应该包含我需要的信息。我可以使用 Chrome 中的开发者工具查看内容。

这是否意味着这里的数据是保护性的,不允许刮取?

还有其他资源和方法可以实现我的目标吗?

提前致谢。

  <table id="top-1000" class="t1k-table styled-table">
    <thead>
      <tr>
        <th class="t1k-rank">Rank</th>
        <th class="t1k-url">URL</th>
        <th class="t1k-pagespeed">PageSpeed</th>
        <th class="t1k-yslow">YSlow</th>
        <th class="t1k-time">Onload</th>
        <th class="t1k-time">Fully Loaded</th>
        <th class="t1k-elements">Requests</th>
        <th class="t1k-size">Total Size</th>
      </tr>
    </thead>
    <tbody>
    </tbody>
  </table>

标签: iosswiftiphoneweb-scrapingscrapy

解决方案


推荐阅读