|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
新人求教 用的spyder win10系统
python versio:3.5.2
请问,那一步哪里错了 还没看出来0.0
- # -*- coding: utf-8 -*-
- """
- Created on Fri Jul 29 14:24:31 2016
- @author: mozzielx
- """
- from urllib.parse import urlencode
- from urllib.request import Request,urlopen
- '''模拟登录'''
- CaptchaUrl = "http://jwxt1.cumt.edu.cn/CheckCode.aspx"#验证码网址
- PostUrl = "http://jwxt1.cumt.edu.cn/default2.aspx"#登录时提交data的页面
- # 用户名和密码
- username = 'xx'
- password = 'xx'
- #获取验证码图片————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————#
- request_pic = Request(CaptchaUrl)
- request_pic.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.84 Safari/537.36')
- response_pic = urlopen(request_pic)
- pic = response_pic.read()
- m_cookie = response_pic.getheader("Set-Cookie")#访问验证码地址,获取cookie
- # 保存验证码到本地
- with open('验证码.jpg', 'wb') as f:
- f.write(pic)
- SecretCode = input('输入验证码: ')# 手动打开保存的验证码图片 输入
- #下面是登录教务处——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————#
- postData_login = {
- '__VIEWSTATE': 'dDwyODE2NTM0OTg7Oz6gIzHUHUo9I4p23LsZjjNrYu9EnQ==',
- 'txtUserName': username,
- 'TextBox2': password,
- 'txtSecretCode': SecretCode, #验证码
- 'RadioButtonList1': '学生',
- 'Button1': '',
- 'lbLanguage': '',
- 'hidPdrs': '',
- 'hidsc': ''
- }
- headers_login = {
- 'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
- 'Accept-Encoding':'gzip, deflate',
- 'Accept-Language':'zh-CN,zh;q=0.8',
- 'Cache-Control':'max-age=0',
- 'Connection':'keep-alive',
- 'Content-Type':'application/x-www-form-urlencoded',
- 'Cookie':m_cookie,
- 'Host':'jwxt1.cumt.edu.cn',
- 'Origin':'http://jwxt1.cumt.edu.cn',
- 'Referer':'http://jwxt1.cumt.edu.cn/',
- 'Upgrade-Insecure-Requests':'1',
- 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.84 Safari/537.36'
- }
- data_login = urlencode(postData_login,encoding='gb2312').encode('gb2312')#因为data含中文,所以encoding='xxxx'
- request_login = Request(PostUrl, data_login, headers_login)
- response_login = urlopen(request_login)
- result_login = response_login.read().decode('gb2312')# 由于该网页是gb2312的编码,所以需要解码
- #print(result_login)# 打印登录后的页面
- #下面是查成绩—————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————#
- url_grade ='http://jwxt1.cumt.edu.cn/xscj_gc.aspx?xh=03141060&xm=%C1%D6%D0%C0&gnmkdm=N121605'#查成绩网址
- postdata_grade ={'__VIEWSTATE':'dDwxODI2NTc3MzMwO3Q8cDxsPHhoOz47bDwwMzE0MTA2MDs+PjtsPGk8MT47PjtsPHQ8O2w8aTwxPjtpPDM+O2k8NT47aTw3PjtpPDk+O2k8MTE+O2k8MTM+O2k8MTY+O2k8MjY+O2k8Mjc+O2k8Mjg+O2k8MzU+O2k8Mzc+O2k8Mzk+O2k8NDE+O2k8NDU+Oz47bDx0PHA8cDxsPFRleHQ7PjtsPOWtpuWPt++8mjAzMTQxMDYwOz4+Oz47Oz47dDxwPHA8bDxUZXh0Oz47bDzlp5PlkI3vvJrmnpfmrKM7Pj47Pjs7Pjt0PHA8cDxsPFRleHQ7PjtsPOWtpumZou+8muacuueUteW3peeoi+WtpumZojs+Pjs+Ozs+O3Q8cDxwPGw8VGV4dDs+O2w85LiT5Lia77yaOz4+Oz47Oz47dDxwPHA8bDxUZXh0Oz47bDzmnLrmorDlt6XnqIs7Pj47Pjs7Pjt0PHA8cDxsPFRleHQ7PjtsPOihjOaUv+ePre+8muacuuaisOW3peeoizIwMTQtMDbnj607Pj47Pjs7Pj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MD47PjtsPHQ8O2w8aTwwPjs+O2w8dDxwPHA8bDxUZXh0Oz47bDxaSVNUOz4+Oz47Oz47Pj47Pj47Pj47dDxAMDw7Ozs7Ozs7Ozs7Pjs7Pjs+Pjs+Pjs+ZvafybAT0oLKL3rUY+aWJ0cpEco=',
- '__VIEWSTATEGENERATOR':'DB0F94E3',
- 'ddlXN':'2015-2016',
- 'ddlXQ':'2',
- 'Button1':'按学期查询'}
- headers_grade = { 'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
- 'Accept-Encoding':'gzip, deflate',
- 'Accept-Language':'zh-CN,zh;q=0.8',
- 'Connection':'keep-alive',
- 'Content-Type':'application/x-www-form-urlencoded',
- 'Cookie':m_cookie,
- 'Host':'jwxt1.cumt.edu.cn',
- 'Origin':'http://jwxt1.cumt.edu.cn',
- 'Referer':'http://jwxt1.cumt.edu.cn/xscj_gc.aspx?xh=03141060&xm=%C1%D6%D0%C0&gnmkdm=N121605',
- 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.84 Safari/537.36'}
- data_grade = urlencode(postdata_grade,encoding='gb2312').encode('gb2312')
- request_grade = Request(url_grade, data_grade, headers_grade)
- response_grade = urlopen(request_grade)
- result_grade = response_grade.read().decode('gb2312')
- print(result_grade)
-
复制代码
下面是报错信息
- runfile('E:/python practice/数据爬取/教务系统.py', wdir='E:/python practice/数据爬取')
- 输入验证码: v523
- Traceback (most recent call last):
- File "<ipython-input-20-e7fcb8a24997>", line 1, in <module>
- runfile('E:/python practice/数据爬取/教务系统.py', wdir='E:/python practice/数据爬取')
- File "F:\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 714, in runfile
- execfile(filename, namespace)
- File "F:\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 89, in execfile
- exec(compile(f.read(), filename, 'exec'), namespace)
- File "E:/python practice/数据爬取/教务系统.py", line 53, in <module>
- response_login = urlopen(request_login)
- File "F:\Anaconda3\lib\urllib\request.py", line 163, in urlopen
- return opener.open(url, data, timeout)
- File "F:\Anaconda3\lib\urllib\request.py", line 466, in open
- response = self._open(req, data)
- File "F:\Anaconda3\lib\urllib\request.py", line 484, in _open
- '_open', req)
- File "F:\Anaconda3\lib\urllib\request.py", line 444, in _call_chain
- result = func(*args)
- File "F:\Anaconda3\lib\urllib\request.py", line 1282, in http_open
- return self.do_open(http.client.HTTPConnection, req)
- File "F:\Anaconda3\lib\urllib\request.py", line 1254, in do_open
- h.request(req.get_method(), req.selector, req.data, headers)
- File "F:\Anaconda3\lib\http\client.py", line 1106, in request
- self._send_request(method, url, body, headers)
- File "F:\Anaconda3\lib\http\client.py", line 1146, in _send_request
- self.putheader(hdr, value)
- File "F:\Anaconda3\lib\http\client.py", line 1082, in putheader
- if _is_illegal_header_value(values[i]):
- TypeError: expected string or bytes-like object
复制代码
TypeError: expected string or bytes-like object
类型错误:预期的是 字符串 或 字节串对象
---------------------
- request_login = Request(PostUrl, data_login, headers_login)
- response_login = urlopen(request_login)
复制代码
urlopen( url) 接受的 url ,是个链接地址字符串。
而你 给了个 Request 对象。
|
|