2012-06-01 186 views
0

当我访问www.sampleweb.com/reg/我有一个输入值类似。如何从python获得网页中的html输入值

<input id="input-id" class="input-class" name="myinput" type="text" value="hello world">

我怎样才能使用python www.sampleweb.com/reg/的输入hello world价值?

我想在访问www.sampleweb.com/reg/是这样的:

url = 'http://www.sampleweb.com/reg/' 
urlopen(url) 

是在访问的URL这正确吗?

任何人都可以帮助我了解我的情况吗?

在此先感谢...

回答

0

请注意,使用DOM Parser是解析出任何资源的HTML的最佳选择。

然而,如果“世界你好”是你想出来的HTML中的唯一的事情,那么快速和肮脏的方法是:

toFind = '<input id="input-id" class="input-class" name="myinput" type="text" value="' 
htmlStr = urllib.urlopen('yoururl.com/your/path').read() 
value = htmlStr[htmlStr.index(toFind)+len(toFind):] 
value = htmlStr[:htmlStr.index('\"')] 
print value