2013-02-23 67 views
0

我想通過抓取way2sms.com發送短信,但我無法使用機械化登錄到way2sms.com。用機械化抓取way2sms

我使用以下代碼提交登錄表單。

import mechanize 
br = mechanize.Browser() 
br.set_handle_robots(False) 
br.set_handle_refresh(False) 
br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; Linux x86_64; rv:18.0) Gecko/20100101 Firefox/18.0')] 
res=br.open('http://wwwa.way2sms.com/content/prehome.jsp') 
link=list(br.links())[5] 
res=br.follow_link(link) 
br.form = list(br.forms())[0] 
br.form.find_control('username').value=USERNAME #user name 
br.form.find_control('password').value=PASSWORD #password 
res=br.submit() 

提交表單後,又收到登錄頁面。

+0

他們可能會在登陸時設置cookie(記錄後您仍然必須擁有該cookie)。嘗試使用禁用的Cookie登錄以確認(請記住先清除舊的Cookie!)。 – 2013-02-23 21:54:27

+0

遲到的答案在這裏。看看我的[腳本](http://code.google.com/p/way2sms-python/):-) – Brijin 2013-09-01 13:10:22

回答

0

只需用您的用戶名和密碼替換用戶名和密碼即可。

import mechanize 
import cookielib 
br = mechanize.Browser() 

# Cookie Jar 
cj = cookielib.LWPCookieJar() 
br.set_cookiejar(cj) 

# Browser options 
br.set_handle_equiv(True) 
br.set_handle_gzip(True) 
br.set_handle_redirect(True) 
br.set_handle_referer(True) 
br.set_handle_robots(False) 

# Follows refresh 0 but not hangs on refresh > 0 
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1) 

# User-Agent 
br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')] 

url = 'http://site25.way2sms.com/content/index.html?' 

#Opening WEbsite 
op = br.open(url) 

#Selection form 
br.select_form(nr=0) 

username = 'mobilenumberhere' 
password = 'passwordhere' 


#Give username and password 
br.form['username'] = username 
br.form['password'] = password 

br.submit() 


#To check whether log in Successful or not 
if username in br.geturl(): 
    print "Login Failed" # Go to way2sms and enter wrong details. You will understand this. 
else: 
    print "Login Successful. You are at ", br.geturl()