写在最前
Github 时不时抽风,老是访问不了,诚然,用科学上网能解决一切这类问题,但是,能不翻墙还是不要翻墙的好。这里采用的是通过修改本地 hosts,配置域名和 IP 映射关系,这样一来,当我们访问 github 时,这些域名可以直接从本地 hosts 文件中获取 IP,而不需要再去 DNS 服务器上询问一圈。从而提高访问速度。
修改 hosts
以 Windows 系统为例,hosts 的路径是 C:\Windows\System32\drivers\etc,
打开后将以下内容复制到文件最下方。保存后,win+X,按A,以管理员权限启动 powershell,输入刷新 DNS 缓存。之后就能变快了……
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| 185.199.108.154 github.githubassets.com 199.232.68.133 camo.githubusercontent.com 199.232.68.133 github.map.fastly.net 199.232.69.194 github.global.ssl.fastly.net 140.82.113.3 gist.github.com 185.199.108.153 github.io 140.82.113.4 github.com 140.82.114.5 api.github.com 199.232.68.133 raw.githubusercontent.com 199.232.68.133 user-images.githubusercontent.com 199.232.68.133 favicons.githubusercontent.com 199.232.68.133 avatars5.githubusercontent.com 199.232.68.133 avatars4.githubusercontent.com 199.232.68.133 avatars3.githubusercontent.com 199.232.68.133 avatars2.githubusercontent.com 199.232.68.133 avatars1.githubusercontent.com 199.232.68.133 avatars0.githubusercontent.com 140.82.114.9 codeload.github.com 52.217.83.84 github-cloud.s3.amazonaws.com 52.216.229.155 github-com.s3.amazonaws.com 52.216.30.60 github-production-release-asset-2e65be.s3.amazonaws.com 52.216.17.0 github-production-user-asset-6210df.s3.amazonaws.com 52.216.236.43 github-production-repository-file-5c1aeb.s3.amazonaws.com
|
保存后,win+X,按A,以管理员权限启动 powershell,输入
刷新 DNS 缓存。之后就能变快了……
才怪嘞,因为上述域名的 IP 是不停在变的,这一串映射仅仅适用于今天而已,到了明天,ip 一变动,又登不上了,我总不可能天天来更新吧?
使用爬虫脚本实时获取最新 ip
因为 github 相关的诸多域名的 ip 是在变动的,为了能够实时获取最新的 ip,我们可以使用爬虫来从站长之家或其余 ip 查询网站上爬取最新 ip。
这里要用到 python,安装推荐使用 Anaconda,Anaconda 安装方式推荐参看 Win10 重装日记的 3.2 章节。根据 Anaconda 版本与 python 版本的对照选择自己需要的 Anaconda 安装包,然后直接安装即可,路径建议修改为非系统盘,同时务必勾选将 python 路径加入到环境变量的选项。
新建一个 python 脚本 ——githosts.py(最简单的方法,新建一个 txt 文本文件,把下面的代码粘贴进去以后,修改后缀为.py)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
| #!/usr/bin/env python
s = """ github.githubassets.com camo.githubusercontent.com github.map.fastly.net github.global.ssl.fastly.net gist.github.com github.io github.com api.github.com raw.githubusercontent.com user-images.githubusercontent.com favicons.githubusercontent.com avatars5.githubusercontent.com avatars4.githubusercontent.com avatars3.githubusercontent.com avatars2.githubusercontent.com avatars1.githubusercontent.com avatars0.githubusercontent.com codeload.github.com github-cloud.s3.amazonaws.com github-com.s3.amazonaws.com github-production-release-asset-2e65be.s3.amazonaws.com github-production-user-asset-6210df.s3.amazonaws.com github-production-repository-file-5c1aeb.s3.amazonaws.com """
import requests from bs4 import BeautifulSoup import os
ans = [] for i in s.split(): try: url = "http://ip.tool.chinaz.com/" + i.strip() resp = requests.get(url) soup = BeautifulSoup(resp.text,"html5lib") x = soup.find(class_="IcpMain02") x = x.find_all("span", class_="Whwtdhalf") x = "%s %s" % (x[5].string.strip(), i.strip()) print(x) ans.append(x) except: print("返回值为null,查询失败。请确保域名正确")
hosts = r"C:\Windows\System32\drivers\etc\hosts" with open(hosts, "r") as f: content = [i for i in f.readlines() if i.startswith("#")] content.extend(ans) with open(hosts, "w") as f: f.write("\n".join(content))
os.system('ipconfig /flushdns')
|
然后把 githosts.py 放到 C:\Windows\System32 目录下(不放其实也可以,只是这样一来每次都要自己 cd 路径。)
win+X,按A, 以管理员权限启动 Powershell,输入
然后脚本就会自动爬取最新的 IP 与域名到你的 hosts 文件中了。