python脚本模拟浏览器
(偶尔看到,怕忘了)
仿用户打开浏览器然后点击等行为然后获取结果,以下是我使用过的方法只是依赖与ie不过firefox等应该也有相应的调用方法:
思路就是调用ie的com组件然后就是对dom的操作跟用javascript操作dom类似,示范代码如下
#天涯登陆地址
tianyalogin = "http://www.tianya.cn/"
tianya_user = "xxxxx"
tianya_pw = "xxxxx"
ie = win32com.client.Dispatch("InternetExplorer.Application")
ie.Visible = 0
#开始登陆
ie.Navigate(tianyalogin)
state = ie.ReadyState
print "打开登陆页面"
while 1:
state = ie.ReadyState
if state ==4:
break
sleep(1)
print "页面载入完毕,输入用户名密码"
state = None
ie.Document.getElementById("text1").value=tianya_user
ie.Document.getElementById("password1").value=tianya_pw
ie.Document.getElementById("button1").click()
while 1:
state = ie.ReadyState
print state
if state ==4 and str(ie.LocationURL) == "http://
cache.tianya.cn/index.htm":
break
sleep(1)
print "登陆成功"
相关文档:
client:
import socket, sys
if __name__ == '__main__':
#处理参数
argv = sys.argv
if (len(argv)!=3) or (len(argv)==2 and argv[1]=='/?'):
print '>>>Useage:', argv[0], '<address> < ......
Pythonwin - Python IDE and GUI Framework for Windows.
Copyright 1994-2006 Mark Hammond
Python is Copyright (c) 2000-2008 ActiveState Software Inc.
Copyright (c) 2001-2008 Python Software Foundation.
All Rights Reserved.
Copyright (c) 2000 BeOpen.com.
All Rights Reserved.
Copyright (c) 1995-20 ......
在Python中有些特殊的地方是存在两种字符串,分别为str和unicode字符串,他们都继承自basestring。
如:s="hello world",s为str;us=u"hello world",us为unicode。
使用help(str)和help(unicode)可以查看各自说明,他们都有decode、encode方法,
decode用于将str字符串解码为unicode字符串,
encode用于将unicode字符 ......
今天一不小心把Python给卸载掉了,导致emerge不能使用,最终找到如下解决方案:
wget http://www.python.org/ftp/python/2.4.4/Python-2.4.4.tar.bz2\
tar xjvf Python-2.4.4.tar.bz
cd Python-2.4.4
./configure –with-fpectl –infodir=/usr/share/info/ –mandir=/usr/share/man
make
make instal ......
>>> a = {'1':'2'}
>>> b = {'3':'4'}
>>> a+b
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'dict' and 'dict'
>>> a.update(b)
>>> a
{'1': '2', '3': '4'} ......