最近學習python,因常常登陸公積金網站查看公積金繳存還款狀況,so網上找了寫腳本,修改了一下,方便獲取網頁中的數據。html
使用谷歌瀏覽器F12查看登陸請求內容java
1.request header須要參數:User-Agent、Referer等。python
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('<td><p>繳至月份:</p>(\s*)</td>(\s*)<td>(.*?)</td>', html) #使用正則表達式匹配繳至月份 print(hgjj_last_data[0][2])
referer:http://www.blogjava.net/hongqiang/archive/2012/08/01/384552.html