Swift如何从实时网站中抓取排名前500或1000的热门网站?

时间:2020-09-24 19:08:56

标签: ios swift iphone web-scraping scrapy

首先,我不知道从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>

0 个答案:

没有答案