if let url = URL(string: "https://stackoverflow.com/") {
URLSession.shared.dataTask(with: url) { data, resp, error in
if let error = error {
print(error.localizedDescription)
}
if let data = data {
let str = String(data: data, encoding: .utf8)
print(str as Any)
}
}
.resume()
}
字符串 您可以从下面的字符串中突出显示的行中获取标题和图像:
<!DOCTYPE html>
<html>
<head>
EXTRACT TITLE FROM HERE --->
<title>Stack Overflow</title>
EXTRACT ICON FROM HERE (href link) --->
<link rel="shortcut icon" href="//cdn.sstatic.net/Sites/stackoverflow/img/favicon.ico?v=4f32ecc8f43d">
<link rel="apple-touch-icon image_src" href="//cdn.sstatic.net/Sites/stackoverflow/img/apple-touch-icon.png?v=c78bd457575a">
<link rel="search" type="application/opensearchdescription+xml" title="Stack Overflow" href="/opensearch.xml">
<meta name="twitter:card" content="summary">
<meta name="twitter:domain" content="stackoverflow.com"/>
<meta property="og:type" content="website" />
<meta property="og:image" itemprop="image primaryImageOfPage" content="http://cdn.sstatic.net/Sites/stackoverflow/img/[email protected]?v=73d79a89bded&a" />
<meta name="twitter:title" property="og:title" itemprop="title name" content="Stack Overflow" />
<meta name="twitter:description" property="og:description" itemprop="description" content="Q&A for professional and enthusiast programmers" />
<meta property="og:url" content="http://stackoverflow.com/"/>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="//cdn.sstatic.net/Js/stub.en.js?v=b84e3ec1d0b3"></script>
<link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/Sites/stackoverflow/all.css?v=ef9c49b839e0">
<link rel="alternate" type="application/atom+xml" title="Feed of recent questions" href="/feeds">
<script>
1条答案
按热度按时间j8ag8udp1#
你可以像这样从URL中获取数据:
字符串
您可以从下面的字符串中突出显示的行中获取标题和图像:
型