代码拉取完成,页面将自动刷新
#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(" ",' ').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()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。