关于mechanize模拟浏览器(HDU提交代码)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/PoorGuy_tn/article/details/81871780

关于使用mechanize向hdu等oj自动提交代码教程:

首先当大家学到mechanize想必大家已经对python有一定的了解了,下面直接给实现方法:

import mechanize
import sys

reload(sys)
sys.setdefaultencoding('utf-8')
br = mechanize.Browser()
cj = mechanize.CookieJar()
#options
br.set_handle_equiv(True)
br.set_handle_gzip(False)
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)

#br.set_debug_http(True)
#br.set_debug_redirects(True)
#br.set_debug_responses(True)
#欺骗行为
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')]
br.set_cookiejar(cj)
br.open('http://acm.hdu.edu.cn')
br.select_form(nr = 2)
br.form['username'] = '×××××' #该处填写hdu用户名
br.form['userpass'] = '×××××' #该处填写hdu账号
br.submit()
br.open('http://acm.hdu.edu.cn/submit.php?pid')
for i in xrange(10):  #该处为需要提交的次数
    br.select_form(nr = 2)
    br.form['problemid'] = '1000'  
    f = open('/home/poorguy/Desktop/code.txt')  #该处为代码存放位置,请自行更改
    code = f.read()
    f.close()
    br.form['usercode'] = code
    br.submit()
    br.back()

注意想要运行这个代码首先要有mechanize模块:

mechanize安装教程请看:
https://blog.csdn.net/PoorGuy_tn/article/details/81872195

直接在python中输入以上代码即可提交:

注意:本文的代码存放在桌面的code.txt中,本代码为最基础的machanize,想深入学习请大家自行学习

本文只可用于学习交流,不可对任何网站进行恶意攻击,不得妨碍网站的正常运转

如果大家有好的建议,欢迎分享

小白创作,大佬勿喷,谢谢合作

猜你喜欢

转载自blog.csdn.net/PoorGuy_tn/article/details/81871780
今日推荐