展开全部
使用谷e68a843231313335323631343130323136353331333363393638歌浏览器F12查看登录请求内容
1.request header需要参数:User-Agent、Referer等。
2.post内容。
python 3.x中urllib库和urilib2库合并成了urllib库。
urllib2.urlopen()变成了urllib.request.urlopen()
urllib2.Request()变成了urllib.request.Request()
cookielib 模块-》http.cookiejar#! /usr/bin/env python
# -*- coding:gb2312 -*-
# __author__="zhaowei"
'''
python3.4
模拟登录郑州公积金网站,查询缴存至月份。
'''
from html.parser import HTMLParser
import urllib
import http.cookiejar
import string
import re
hosturl = 'http://www.zzgjj.com/index.asp'
posturl = 'http://www.zzgjj.com/user/login.asp'
cj = http.cookiejar.CookieJar()
cookie_support = urllib.request.HTTPCookieProcessor(cj)
opener = urllib.request.build_opener(cookie_support, urllib.request.HTTPHandler)
urllib.request.install_opener(opener)
h = urllib.request.urlopen(hosturl)
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0',
'Referer': 'http://www.zzgjj.com/index.asp'}
postData = {'selectlb': '1',#登录模式,身份证2,账号1
'username': '1423141234', #公积金账号
'radename': '赵威',#姓名
'mm': '88888',#密码
'submit322': '确认'#固定值
}
postData = urllib.parse.urlencode(postData, encoding='gb2312').encode('gb2312')
#因为post里面有中文,因此需要先把url经过gb2312编码处理,然后再把请求编码为gb2312字节码(post必须是字节码)。
request = urllib.request.Request(posturl, postData, headers)
response = urllib.request.urlopen(request)
text = response.read()
html = text.decode('gb2312')
hgjj_last_data = re.findall('
缴至月份:
(\s*)(\s*)(.*?)', html)#使用正则表达式匹配缴至月份
print(hgjj_last_data[0][2])