访问google的python脚本

转载自: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'

亲测可用:

发布了36 篇原创文章 · 获赞 39 · 访问量 19万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章