2012-06-01 188 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