当前位置:编程学习 > 网站相关 >>

python 最简单的破除网页限制实例代码

网页中增加限制,无非是在 html 中设置脚本,既然浏览器可以显示出来,就一定能得到它的文本。
第一步,在 python shell 中执行:
>>> import urllibeast inflatables
>>> urllib.urlretrieveInflatable Castle urlretrieve 可以把一个网页保存到本地文件。

第二步,分析这个 tmp.html 文件,发现其中的  < body > 标签比较恶心:
body leftmargin=0 topmargin=0 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
(注意, < > 在这里省略了)
浏览Inflatable Slide Inflatable Toys这个文件, ok 限制解除。

第三步, 自动下载外贸学习 网页,进行"净化"处理, 编写一个python  程序:
import urllib
urls =
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 和 相应的本地文件名, 使用者可以根据自己的情况添加。
 
        注意,这个程序是专门针对这个外贸软件网站的, 对于其他外贸问题的网站,可能使用的方法会有不同,但是按照上面的步骤,相信大家都能搞定。
补充:Web开发 , Python ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,