|

楼主 |
发表于 2019-3-8 23:33:49
|
显示全部楼层
本帖最后由 t6am3 于 2019-3-8 23:48 编辑
贴一点代码。。我是新手,,但是实在没想通咋回事
- import requests
- import datetime
- import os
- from vertificationCodeReg import vertificationCode
- from PIL import Image
- from PIL import ImageEnhance
- from bs4 import BeautifulSoup as bs
- s = requests.Session()
- def login():
- initialUrl = ##不显示##
- initHeaders = {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8', 'Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8', 'Connection': 'keep-alive', 'Host': 'gsmis.graduate.buaa.edu.cn', 'Upgrade-Insecure-Requests': '1', 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'}
- s.get(initialUrl, headers=initHeaders)
- headers = {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
- 'Accept-Encoding': 'gzip, deflate',
- 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
- 'Cache-Control': 'max-age=0',
- 'Connection': 'keep-alive',
- 'Content-Length': '45',
- 'Content-Type': 'application/x-www-form-urlencoded',
- 'Host': ##不显示##
- 'Origin': ##不显示##
- 'Referer': ##不显示##
- 'Upgrade-Insecure-Requests': '1',
- 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36',
- }
- JSESSIONID = s.cookies.get_dict()['JSESSIONID']
- headers[ 'Cookie'] = f'JSESSIONID={JSESSIONID}; _ga=GA1.3.1234387313.1538569273; Hm_lvt_8edeba7d3ae859d72148a873531e0fa5=1539923770,1542172342; UM_distinctid=16771ed7aac159-028525f4a66be6-3a3a5d0c-1fa400-16771ed7aad774; CASTGC=TGT-189864-kqSVcobA7GQmbUEG0qmKaEGETmevCpUZL7Tv6VRWOi5HHRY61w-cas'
- loginUrl = ##不显示##
- data = {
- #此部分为隐私
- }
- s.post(loginUrl, headers=headers, data=data)
- def gradeQuery():
- headers = {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
- 'Accept-Encoding': 'gzip, deflate',
- 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8',
- 'Connection': 'keep-alive',
- 'Host': '##不显示##
- 'Referer':##不显示##
- 'Upgrade-Insecure-Requests': '1',
- 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'}
- url = ##不显示##
- #print('query', s.cookies.get_dict())
-
- # 使用这个获取不到response
- JSESSIONID1 = s.cookies.get_dict()['JSESSIONID']
- print("这是Session的cookie", JSESSIONID1)
- # 使用这个获取的到response
- JSESSIONID2 = 'D82FCE15DF573FBEE86A4A7C2FD470CA.tomcat77_1'
- print("这是浏览器的cookie", JSESSIONID2)
- print("长度相等吗?", len(JSESSIONID1)==len(JSESSIONID2))
- headers['Cookie'] = f'JSESSIONID={JSESSIONID1}; _ga=GA1.3.1234387313.1538569273; Hm_lvt_8edeba7d3ae859d72148a873531e0fa5=1539923770,1542172342; UM_distinctid=16771ed7aac159-028525f4a66be6-3a3a5d0c-1fa400-16771ed7aad774; CASTGC=TGT-189864-kqSVcobA7GQmbUEG0qmKaEGETmevCpUZL7Tv6VRWOi5HHRY61w-cas'
- response = requests.get(url, headers=headers)
- print(response.headers)
- soup = bs(response.text, features="lxml")
- with open('response1.html', 'w') as f:
- f.write(response.text)
- #print(soup)
- #parseSoup(soup)
复制代码 |
|