用机械化刮刮2sms

时间:2013-02-23 16:55:22

标签: python login screen-scraping mechanize-python way2sms

我正在尝试通过抓住way2sms.com发送短信,但我无法使用mechanize登录way2sms.com。

我使用以下代码提交登录表单。

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()

提交表单后,再次收到登录页面。

1 个答案:

答案 0 :(得分: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()