scraping way2sms with mechanize

无人久伴 提交于 2019-12-25 01:54:01

问题


I am trying to send an sms with by scraping way2sms.com, but I am unable to login into way2sms.com using mechanize.

I am using following code to submit the login form.

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

After submitting the form, again the login page is received.


回答1:


Just replace username and password with your username and password.

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


来源:https://stackoverflow.com/questions/15043035/scraping-way2sms-with-mechanize

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!