<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <title>suger77 (小淘米)</title>
    <link>https://ruby-china.org/suger77</link>
    <description>生活奇奇怪怪，还有我比较可爱</description>
    <language>en-us</language>
    <item>
      <title>爬虫入门基础-HTTP 协议过程</title>
      <description>&lt;p&gt;当我们在浏览器中输入 URL 后，会发生什么？比如&lt;a href="https://www.baidu.com" rel="nofollow" target="_blank"&gt;https://www.baidu.com&lt;/a&gt;
我们可以用 curl -v &lt;a href="https://www.baidu.com" rel="nofollow" target="_blank"&gt;https://www.baidu.com&lt;/a&gt; 来研究这个过程，会发现总共有 4 个子过程。
1、查找域名对应的 IP 地址&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Rebuilt URL to: &lt;a href="https://www.baidu.com/" rel="nofollow" target="_blank"&gt;https://www.baidu.com/&lt;/a&gt;*   Trying 14.215.177.38...
我们可以通过 nslookup &lt;a href="http://www.baidu.com%E6%A8%A1%E6%8B%9F%E8%BF%99%E4%B8%AA%E8%BF%87%E7%A8%8B%EF%BC%8Cdns%E6%9C%8D%E5%8A%A1%E5%99%A8%E8%BF%94%E5%9B%9E%E4%B8%A4%E4%B8%AAIP%E5%9C%B0%E5%9D%80%EF%BC%8Ccurl%E4%BC%9A%E9%9A%8F%E6%9C%BA%E9%80%89%E7%94%A8%E5%85%B6%E4%B8%AD%E4%B8%80%E4%B8%AAIP%E6%9C%8D%E5%8A%A1%E5%99%A8%E8%BF%9B%E8%A1%8C%E8%AE%BF%E9%97%AE%E3%80%82" title=""&gt;www.baidu.com 模拟这个过程，dns 服务器返回两个 IP 地址，curl 会随机选用其中一个 IP 服务器进行访问。&lt;/a&gt;
&lt;img src="https://l.ruby-china.com/photo/2020/cf1c0ad9-3f75-4ef2-85d3-4ccf6acc6d8a.png!large" title="" alt=""&gt;
&lt;img src="https://l.ruby-china.com/photo/2020/77191dc0-9ea8-44d7-803c-0ac41bd022a4.png!large" title="" alt=""&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2、向 IP 对应的服务器发送 SSL 协商请求，进行 SSL 协商&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Connected to &lt;a href="http://www.baidu.com" rel="nofollow" target="_blank" title=""&gt;www.baidu.com&lt;/a&gt; (14.215.177.38) port 443 (#0)
下图是通过 Wireshark 抓包获取的协商过程。
&lt;img src="https://l.ruby-china.com/photo/2020/e05ce92a-ca03-45ad-823b-e769407bef70.png!large" title="" alt=""&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3、SSL 协商完成后，向 IP 对应服务器发起 GET 请求&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;GET / HTTP/1.1
Host: &lt;a href="http://www.baidu.com" rel="nofollow" target="_blank" title=""&gt;www.baidu.com&lt;/a&gt;
User-Agent: curl/7.54.0
网站会检查是不是真的浏览器访问。需加上 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.1276.73 Safari/537.36，表明你是浏览器访问即可。有时还会检查是否带Referer信息还会检查你的Referer是否合法。
&lt;img src="https://l.ruby-china.com/photo/2020/4e33d46c-4a6b-48eb-bc37-e9a0185793cd.png!large" title="" alt=""&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;4、服务器响应请求，发回网页内容
&amp;lt; HTTP/1.1 200 OK
&amp;lt; Connection: keep-alive
&amp;lt; Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/
&amp;lt;
&amp;lt;!DOCTYPE html&amp;gt;...&lt;/p&gt;</description>
      <author>suger77</author>
      <pubDate>Wed, 06 May 2020 17:32:57 +0800</pubDate>
      <link>https://ruby-china.org/topics/39835</link>
      <guid>https://ruby-china.org/topics/39835</guid>
    </item>
  </channel>
</rss>
