解决谷歌搜索技术文章时打不开网页问题的python脚本

yipeiwu_com5年前Python基础
注意:Win7或者WIn8用户要用管理员权限执行。

项目地址:http://code.google.com/p/my-hosts-file/downloads

复制代码 代码如下:

import urllib 
    import os 
    import shutil 

    hostspath = "C:\\Windows\\System32\\drivers\\etc" 
    savepath = hostspath + "\\hostsave" 

    def download_hosts(url = "http://my-hosts-file.googlecode.com/svn/trunk/hosts"): 
        os.chdir(hostspath) 
        if os.getcwd() != hostspath: 
            print("Switch Dir to System32 Error,check permission!\npwd:"+os.getcwd()) 
            exit()  
        try: 
            urllib.urlretrieve(url, "hostsave") 
        except: 
            print '\t Error when retrieveing hosts file from url: ', url 

    def backup_hosts(): 
        shutil.copy("hosts","hosts.bak") 

    def replace_hosts(): 
        shutil.copy("hostsave", "hosts") 
        print("Replace original hosts file finished, then flush dns...") 
        os.remove(savepath)     
        os.system("ipconfig /flushdns") 

    def main(): 
        download_hosts() 
        backup_hosts() 
        replace_hosts() 
    if __name__ == '__main__': 
        main()

相关文章

Python csv模块使用方法代码实例

这篇文章主要介绍了Python csv模块使用方法代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 import csv d...

Python2.7读取PDF文件的方法示例

本文实例讲述了Python2.7读取PDF文件的方法。分享给大家供大家参考,具体如下: 这篇文章示例代码采用的Python版本是2.7,需要下载的插件是PDFMiner,下载地址是htt...

Python 绘图和可视化详细介绍

Python之绘图和可视化 1. 启用matplotlib 最常用的Pylab模式的IPython(IPython --pylab) 2. matplotlib的图像都位于Figure对...

从Python的源码浅要剖析Python的内存管理

从Python的源码浅要剖析Python的内存管理

Python 的内存管理架构(Objects/obmalloc.c): 复制代码 代码如下:     _____   ______&nb...

scrapy-redis的安装部署步骤讲解

先说下自己的环境,redis是部署在centos上的,爬虫运行在windows上, 1. 安装redis yum install -y redis 2. 修改配置文件 vi /et...