使用python破除网页限制

王朝html/css/js·作者佚名  2008-05-19
窄屏简体版  字體: |||超大  

昨天找到一个比较好的人工智能网站, 其中有一些很不错的 prolog 文章,我很感兴趣。文中有很多示例程序,可是网页的右键被禁止了,也不能选择,不能保存,不能察看源代码!!

??? 实在不爽,信息本来就要共享嘛!

??? 只好发扬下 hack 精神,冲破限制。还好,有 python 方便多了。

??? 网页中增加限制,无非是在 html 中设置脚本,既然浏览器可以显示出来,就一定能得到它的文本。

第一步,在 python shell 中执行:

>>> import urllib

>>> urllib.urlretrieve("http://www.chinaai.org/Article_Show.asp?ArticleID=315","c:/tmp.html")

urlretrieve 可以把一个网页保存到本地文件。

第二步,分析这个 tmp.html 文件,发现其中的?< body > 标签比较恶心:

body leftmargin=0 topmargin=0 onmousemove='HideMenu()' oncontextmenu="return false" ondragstart="return false" onselectstart ="return false" onselect="document.selection.empty()" oncopy="document.selection.empty()" onbeforecopy="return false" onmouseup="document.selection.empty()"

把这个标签换成比较干净的:body leftmargin=0 topmargin=0 onmousemove='HideMenu()'

(注意, < > 在这里省略了)

浏览这个文件, ok 限制解除。

第三步, 自动下载网页,进行“净化”处理, 编写一个python?程序:

import urllib

urls = {'http://www.chinaai.org/Article_Show.asp?ArticleID=315':'prolog2.html'}

new_tag = ""

for url in urls:

??filename = urls[url]

??urllib.urlretrieve(url,filename)

??f = open(filename,'r')

??content = f.read()

??f.close()

??l_pos = content.find('<body')

??r_pos = content.find('>', l_pos)

??cont1 = content[:l_pos]

??cont2 = content[r_pos + 1:]

??content = cont1 + new_tag + cont2

??f = open('tmp.html','w')

??f.write(content)

??f.close()

程序中 urls 是一个 字典,里面是 url 和 相应的本地文件名, 使用者可以根据自己的情况添加。

注意,这个程序是专门针对这个网站的, 对于其他的网站,可能使用的方法会有不同,但是按照上面的步骤,相信大家都能搞定。

我们的口号是,“还我自由!”

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航