1 Star 0 Fork 0

RTsien / Code Fragments

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
update_google_hosts_win.py 1.04 KB
一键复制 编辑 原始数据 按行查看 历史
RTsien 提交于 2014-12-20 16:08 . new file
#coding=utf8
import urllib, BeautifulSoup
header = "#请不要在下面边添加任何条目,否则将在下次更新时失效!\n"
web=urllib.urlopen("http://www.360kb.com/kb/2_122.html")
soup=BeautifulSoup.BeautifulSoup(web.read())
new=soup.find(id="storybox")("p")[-1]
new_content=header+str(new).replace("&nbsp;",' ').replace("<br />","")[5:-5]
''' 备用方案
web=urllib.urlopen("http://git.oschina.net/jiange1236/googlehosts/raw/master/hosts141218.txt")
new_content=header+web.read()
'''
hosts=open("C:\Windows\System32\drivers\etc\hosts",'r')
old_content=hosts.read()
hosts.close()
hosts=open("C:\Windows\System32\drivers\etc\hosts",'w')
if old_content.find(header)<0:
if old_content[-1]!='\n\n\n':
content=old_content+'\n\n\n'+new_content
else:
content=old_content+new_content
else:
old_content=old_content[:old_content.find(header)]
content=old_content+new_content
content=unicode(content,"utf8").encode("gb2312")
hosts.write(content)
hosts.close()
print u"更新成功"
raw_input()
C
1
https://gitee.com/rtsien/Code-Fragments.git
git@gitee.com:rtsien/Code-Fragments.git
rtsien
Code-Fragments
Code Fragments
master

搜索帮助