HTTP (HyperText Transfer Protocol,超文本傳輸協定)
超文字(hypertext):在網路上點擊一個連結,會被帶到另外一個地方,因此該連結就不是純文字,而是被稱為超文字。
全世界說好在網路上的資料要如何傳輸,規範要如何發送請求,伺服器要如何應答......等等。
HTTPs (HyperText Transfer Protocol Secure,超文本傳輸安全協定)
HTML (HyperText Markup Language,超文本標記語言)是一種標記語言,而不是程式語言。
每個HTML頁面由兩部分組成:head and body。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body></body>
</html>
基本架構的標籤
每個html標籤都包含兩個部分:starting tag和closing tag,並將內容包在兩個部分中間。
head tag中基本會放的東西:
body tag中的常用標籤:
有屬性的標籤:
import requests
result = requests.get('<https://example.com/>')
print(type(result))
# <class 'requests.models.Response'>
print(result.headers)
# {'Content-Encoding': 'gzip', 'Accept-Ranges': 'bytes', 'Age': '333098', 'Cache-Control': 'max-age=604800', 'Content-Type': 'text/html; charset=UTF-8', 'Date': 'Wed, 19 Oct 2022 08:01:22 GMT', 'Etag': '"3147526947"', 'Expires': 'Wed, 26 Oct 2022 08:01:22 GMT', 'Last-Modified': 'Thu, 17 Oct 2019 07:18:26 GMT', 'Server': 'ECS (sab/5783)', 'Vary': 'Accept-Encoding', 'X-Cache': 'HIT', 'Content-Length': '648'}
print(result.encoding)
# UTF-8
print(result.cookies)
# <RequestsCookieJar[]>
print(result.status_code)
# 200
print(result.text)
# 結果
<!doctype html>
<html>
<head>
<title>Example Domain</title>
<meta charset="utf-8" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style type="text/css">
body {
background-color: #f0f0f2;
margin: 0;
padding: 0;
font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
}
div {
width: 600px;
margin: 5em auto;
padding: 2em;
background-color: #fdfdff;
border-radius: 0.5em;
box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02);
}
a:link, a:visited {
color: #38488f;
text-decoration: none;
}
@media (max-width: 700px) {
div {
margin: 0 auto;
width: auto;
}
}
</style>
</head>
<body>
<div>
<h1>Example Domain</h1>
<p>This domain is for use in illustrative examples in documents. You may use this
domain in literature without prior coordination or asking for permission.</p>
<p><a href="<https://www.iana.org/domains/example>">More information...</a></p>
</div>
</body>
</html>