首页 > 设计 > 软件应用 > 正文

使用python破除网页限制

2018-10-16 21:05:29
字体:
来源:转载
供稿:网友

昨天找到一个比较好的人工智能网站, 其中有一些很不错的 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"

把这个标签换成比较干净的: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 和 相应的本地文件名, 使用者可以根据自己的情况添加。

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

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

本文作者:
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表