昨天找到一個比較好的人工智能網站, 其中有一些很不錯的 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 和 相應的本地文件名, 使用者可以根據自己的情況添加。
注意,這個程序是專門針對這個網站的, 對于其他的網站,可能使用的方法會有不同,但是按照上面的步驟,相信大家都能搞定。
我們的口號是,“還我自由!”
本文作者:新聞熱點
疑難解答