python爬虫解决Github访问速度慢


爬虫解决Github访问速度慢目录

  • 说明
  • 实现过程
  • 代码下载

说明 由于GitHub收到DNS污染,常常会无法登陆、访问速度慢。
可以采用网络上的一些方法,更改hosts。
但是GitHub的IP地址时不时会发生变化,使用ping有时又ping不通。单独查询每个IP地址又很麻烦。
故此使用python爬虫进行批量查询。
实现过程 1.通过https://www.ipaddress.com/可以查询到一系列GitHub网址的IP。通过开发者工具可以得到IP地址在网页中的位置。
python爬虫解决Github访问速度慢
文章图片

2.使用python爬虫进行批量请求,将获得的IP地址保存下来。
import requests from bs4 import BeautifulSoup import os import subprocessclass gitip: def __init__(self, ip_list): super().__init__() self.ip_list = ip_list self.header = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36'} self.ip_1 = 'https://github.com.ipaddress.com/' # github.com self.ip_2 = 'https://github.com.ipaddress.com/gist.github.com' # gist.github.com self.ip_3 = 'https://github.com.ipaddress.com/assets-cdn.github.com' # assets-cdn.github.com self.ip_4 = 'https://site.ip138.com/raw.githubusercontent.com/' # raw.githubusercontent.com def get_1(self): # github.com response = requests.get(self.ip_1, headers = self.header) soup = BeautifulSoup(response.text, features = 'lxml') self.ip_list.append(soup.find_all('ul', {'class': 'comma-separated'})[0].text + 'github.com') def get_2(self): response = requests.get(self.ip_2, headers = self.header) soup = BeautifulSoup(response.text, features = 'lxml') self.ip_list.append(soup.find_all('ul', {'class': 'comma-separated'})[0].text + 'gist.github.com') def get_3(self): response = requests.get(self.ip_3, headers = self.header) soup = BeautifulSoup(response.text, features = 'lxml') ips = soup.find_all('li') for i in range(4): self.ip_list.append(ips[i].text + 'assets-cdn.github.com') def get_4(self): response = requests.get(self.ip_4, headers = self.header) soup = BeautifulSoup(response.text, features = 'lxml') ip = soup.find_all('a', {'target': '_blank'})[25].text self.ip_list.append(ip + 'raw.githubusercontent.com') self.ip_list.append(ip + 'gist.githubusercontent.com') self.ip_list.append(ip + 'cloud.githubusercontent.com') self.ip_list.append(ip + 'camo.githubusercontent.com') self.ip_list.append(ip + 'avatars0.githubusercontent.com') self.ip_list.append(ip + 'avatars1.githubusercontent.com') self.ip_list.append(ip + 'avatars2.githubusercontent.com') self.ip_list.append(ip + 'avatars3.githubusercontent.com') self.ip_list.append(ip + 'avatars4.githubusercontent.com') self.ip_list.append(ip + 'avatars5.githubusercontent.com') self.ip_list.append(ip + 'avatars6.githubusercontent.com') self.ip_list.append(ip + 'avatars7.githubusercontent.com') self.ip_list.append(ip + 'avatars8.githubusercontent.com')if __name__ == '__main__': ip_list = [] error = 0 github = gitip(ip_list) try: github.get_1() except: print('github.com 申请出错') error+=1 try: github.get_2() except: print('gist.github.com 申请出错') error+=1 try: github.get_3() except: print('assets-cdn.github.com 申请出错') error+=1 try: github.get_4() except: print('raw.githubusercontent.com 申请出错') error+=1 # print(github.ip_list) if error == 0: for i in github.ip_list: print(i) try: subprocess.run("explorer.exe %s" % 'C:\Windows\System32\drivers\etc') except: print('请打开文件路径 C:\Windows\System32\drivers\etc 更改hosts文件') os.system('pause')

  • 注:其中raw.githubusercontent.com这个网址在https://www.ipaddress.com/也无法查出IP地址,故转用https://site.ip138.com/。
3.当查询结束之后,程序将自动打开文件路径 C:\Windows\System32\drivers\etc 。
try: subprocess.run("explorer.exe %s" % 'C:\Windows\System32\drivers\etc') except: print('请打开文件路径 C:\Windows\System32\drivers\etc 更改hosts文件')

此时只需更改这个目录下的hosts文件即可。(在hosts文件末尾添加)
python爬虫解决Github访问速度慢
文章图片

修改完hosts之后别忘了在cmd中刷新DNS!!!
修改完hosts之后别忘了在cmd中刷新DNS!!!
修改完hosts之后别忘了在cmd中刷新DNS!!!
ipconfig /flushdns

【python爬虫解决Github访问速度慢】python爬虫解决Github访问速度慢
文章图片

代码下载 1.python代码以及打包好的exe文件已经存放在我的GitHub(欢迎Star~)。
2.如果你的GitHub下载实在太慢,可以移步百度网盘。文件夹名称为“github_ip”。
链接:https://pan.baidu.com/s/19xkAQXn4RZK039EG9z9SVw 提取码:xgaz

    推荐阅读