轉載自:http://www.freelycode.com/fcode/gogoogle
前提
使用Python2.*解釋器
Windows用戶
下載下面腳本, 用Python執行它即可. 需要管理員權限.
windows-google.py(由@一聲菊響提供,特此感謝)Linux及Mac用戶
下載腳本: posix-google.py
然後執行如下命令.
sudo pip install requests sudo pip install beautifulsoup4 sudo python posix-google.py
附註:
- 請使用https的方式訪問,比如:https://www.google.com
- 默認可能會跳轉到www.google.com.hk,如果.hk訪問困難,請使用:https://www.google.com/ncr 方式訪問,禁止國別跳轉。
- 請確保在當“前用戶對host可寫”前提下使用,即獲得管理員權限。
- windows hosts文件所在目錄:C:\WINDOWS\system32\drivers\etc\hosts
- Mac os X和Linux hosts文件所在目錄:/etc/hosts
- 爲獲得更好的效果, 可以使用chrome瀏覽器,並在地址欄裏輸入 chrome://flags/,然後查找QUIC,啓用該協議,同時建議啓用SPDY/4,能讓訪問更流暢。
#!/usr/bin/env python
#-*- coding: utf-8 -*-
#璋鋒瓕host淇敼鑴氭湰
#author pop<hipop#126.com>
#date 01/05/015
#
#銆愪嬌鐢ㄨ鏄庛€�
#璇風‘淇濆湪褰撯€滃墠鐢ㄦ埛瀵筯ost鍙啓鈥濆墠鎻愪笅浣跨敤錛�
#AT涓€涓嬶紝姣忓ぉ榪愯涓€嬈℃洿鍋ュ悍錛�
#鏈搧紱佹椋熺敤銆佹媶瑙f垨鎶曞靉鐏腑錛�
#灝忓鐢熻鍦ㄧ洃鎶や漢闄悓涓嬩竴璧蜂嬌鐢紱
#瀛曞鎱庣敤銆�
import sys,os
import urllib,urllib2,re
if __name__ == '__main__':
print u'璋鋒瓕host淇敼鑴氭湰\nauthor pop<hipop#126.com>\n01/05/015\r鏁版嵁錛歨ttp://www.360kb.com/kb/2_122.html'
#load host from 360kb
htmlH = urllib2.urlopen('http://www.360kb.com/kb/2_122.html')
html = htmlH.read()
reg = r'#base services.*#google hosts 2015 end'
hostHtmlRe = re.search(reg, html, re.S)
hostHtml = hostHtmlRe.group()
hostHtml = hostHtml.replace(' ',' ')
hostHtml = hostHtml.replace('<span>', '')
hostHtml = hostHtml.replace('</span>', '')
hostStr = hostHtml.replace('<br />','')
#write host file
f = open('C:\\Windows\\System32\\drivers\\etc\\hosts', 'r+')
hostOld = f.read()
reg = re.compile(r'\r\n#google=.*#google hosts 2015 end', re.S)
hostNew = re.sub(reg, '', hostOld)
hostNew = hostNew + '\r\n#google===========================\r\n' + hostStr
#瀹夊叏璧瘋錛屼笉淇敼account鐩稿叧
reg = re.compile(r'account', re.S)
hostNew = re.sub(reg, 'OOXXaccount', hostNew)
print hostNew
f.seek(0)
f.write(hostNew)
f.close()
print 'ok'
親測可用: