Github访问加速
(审核员麻烦看清楚,Github加速,哪里涉及翻墙了?全文只涉及Github和ipaddress两个网址,都是国内能正常访问的)
Github
利用爬虫技术爬取最快的github访问地址,自动修改Host文件,并刷新DNS,简化操作步骤。
CDN加速访问github操作流程
- 访问ipaddress
- 查询下列三个链接的DNS解析地址
- github.com
- assets-cdn.github.com
- github.global.ssl.fastly.net
- 接着,打开系统hosts文件(需管理员权限)。路径:C:\Windows\System32\drivers\etc
- 添加三行记录并保存
140.82.113.3 github.com
185.199.108.153 assets-cdn.github.com
199.232.69.194 github.global.ssl.fastly.net
- 刷新系统DNS缓存
Windows+X 打开系统命令行(管理员身份)或powershell
运行 ipconfig /flushdns 手动刷新系统DNS缓存。
实现方式
- 使用分别爬取三个链接地址对应的DNS地址(request+BeautifulSoup)
- 写入host文件
- 刷新缓存(os.system)
使用方式
使用python执行main.py文件
python main.py
代码实现
import time
import os
from urllib import request
from bs4 import BeautifulSoup
def getAdder(site, select):
url = "https://www.ipaddress.com/site/" + site
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'}
page = request.Request(url, headers=headers)
page_info = request.urlopen(page).read().decode('utf-8')
# 打开Url,获取HttpResponse返回对象并读取其ResposneBody
# 将获取到的内容转换成BeautifulSoup格式,并将html.parser作为解析器
soup = BeautifulSoup(page_info, 'html.parser')
x = soup.select(select)
adder = x[0].string
return adder
if __name__ == '__main__':
hosts = ["github.com", "assets-cdn.github.com", "github.global.ssl.fastly.net"]
selects = ['body > div.resp.main > main > section:nth-child(7) > table > tbody > tr:nth-child(8) > td > ul > li',
'#dns > tbody > tr:nth-child(1) > td:nth-child(2) > a',
'body > div.resp.main > main > section:nth-child(8) > div > table > tbody > tr:nth-child(6) > td > ul > li:nth-child(1)']
result = ""
print("开始查询.....")
for i in range(len(hosts)):
print("开始查询" + hosts[i])
string = getAdder(hosts[i], selects[i]) + " " + hosts[i]
print(hosts[i] + "查询成功,结果为:" + string)
result = result + string + "\n"
# if i < len(hosts) - 1:
# print("开始睡眠...")
# time.sleep(5)
# print("睡眠结束")
print("写入host文件")
host_file = open('C:\Windows\System32\drivers\etc\hosts', 'w')
host_file.writelines(result)
host_file.close()
print("写入成功")
print("开始刷新缓存...")
os.system('ipconfig /flushdns')
print("刷新完成")