def first_log_cookie(self): login_url = 'http://520myfuture.com/account/login' try: request = requests.Session() response = request.get(url=login_url, headers=HEADERS, timeout=10) if response.status_code == 200: acookie = response.cookies.get_dict() os.makedirs('./Cookies/', exist_ok=True) with open('./Cookies/first_log_cookie.txt', 'w', encoding='utf-8') as f1: for key, value in acookie.items(): f1.write(key + "=" + string(value)) f1.close() with open('./Cookies/first_log_cookie.txt', 'r', encoding='utf-8') as f2: acookie = f2.readline() HEADERS.update(Cookie=acookie, Referer=login_url) dict_3 = {'X-Requested-With': 'XMLHttpRequest'} HEADERS.update(dict_3) f2.close() print(HEADERS) print(acookie) return HEADERS else: print(response.history) except ConnectionError: pass
def cookieupdate(self): try: with open('./Cookies/firstlogCookie.txt', 'r', encoding='utf-8') as f2: self.cookie = f2.readline() print("cookie: " + string(self.cookie)) f2.close() except Exception as e: print(e) pass
def first_login_reqck(self): try: response = requests.Session() self.data1.update(UserName=self.username, PassWord=self.userpassword) res = response.post(url=self.url1, data=self.data1, headers=self.headers1, timeout=10) cookies1 = res.cookies.get_dict() self.cookieId = cookies1.get('CookieId') print('CookieId: ' + string(self.cookieId)) print('firstlogcookie: ' + string(cookies1)) with open('./Cookies/firstlogCookie.txt', 'w', encoding='utf-8') as f: for key, value in cookies1.items(): f.write(key + "=" + string(value) + "; ") f.close() with open('./Cookies/firstlogCookie.txt', 'rb+') as f1: f1.seek(-2, os.SEEK_END) f1.truncate() f1.close() except Exception as e1: print("Error1: " + string(e1)) pass try: self.cookieupdate() self.headers2.update(Cookie=self.cookie) self.data2.update(CookieId=self.cookieId) response1 = requests.Session() res1 = response1.post(url=self.url1, data=self.data2, headers=self.headers2, timeout=10) except Exception as e2: print("error2: " + string(e2)) pass
def dict_format(self, filepath, dict_1): try: with open(filepath, 'w', encoding='utf-8') as f1: for key, value in dict_1.items(): f1.write(key + "=" + string(value) + "; ") f1.close() except FileNotFoundError: print('file not exist') os.mkdir(filepath) self.dict_format(filepath, dict_1) # with open(filepath, 'w', encoding = 'utf-8') as f1: # for key, value in dict_1.items( ): # f1.write(key + "=" + string(value) + "; ") # f1.close( ) pass
import re import ssl from urllib import request from bs4 import BeautifulSoup from soupsieve.util import string url = "F:\BDFB-spider\Sample\\test5.html" # content = urlopen(url).read() context = open(url, 'r', encoding = 'utf-8').read( ) print(type(context)) content = string(BeautifulSoup(context, 'html.parser')) # content = urlopen("https://www.baidu.com").read() # context = ssl._create_unverified_context() # req = request.Request(url = "F:\BDFB-spider\Sample\案例与裁判文书_裁判文书_裁判文书公开_法院判决书_法院裁定书_司法审判书-北大法宝V6官网.html",) # res = request.urlopen(req,context=context) # content = res.read() # print(content) # res_url = r"(?<=href=\").+?(?=\")|(?<=href=\').+?(?=\')" # link = re.findall(res_url, content, re.I|re.S|re.M) # for url in link: # print(url) # req_url = '^a6bdb3332ec0adc4.*bdfb$'<a.*?href="(.*?)">.*?</a> results = re.findall('<input.*?checkbox.*?checkbox.*?value="(a.*?)"/>', content, re.S) print(results) print(len(results)) # for result in results: # print(result[1])
def first_login_reqck(username, userpassword): global cookieID url1 = 'https://www.pkulaw.cn/case/CheckLogin/Login' data1 = { 'Usrlogtype': '1', 'ExitLogin': '', 'menu': 'case', 'CookieId': '', 'UserName': username, 'PassWord': userpassword, 'jz_id': '0', 'jz_pwd': '0', 'auto_log': '0' } data2 = { 'Usrlogtype': '1', 'ExitLogin': '', 'menu': 'case', 'CookieId': 'gwqimjpsnpemccguu4ns3d0d', 'UserName': '', 'PassWord': '', 'jz_id': '', 'jz_pwd': '', 'auto_log': '' } headers1 = { 'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate, br', 'Accept-Language': 'en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7', 'Connection': 'keep-alive', 'Content-Length': '113', 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', 'DNT': '1', 'Host': 'www.pkulaw.cn', 'Origin': 'https://www.pkulaw.cn', 'Referer': 'https://www.pkulaw.cn/Case/', 'sec-ch-ua': '"Google Chrome 79"', 'Sec-Fetch-Dest': 'empty', 'Sec-Fetch-Mode': 'cors', 'Sec-Fetch-Site': 'same-origin', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36', 'X-Requested-With': 'XMLHttpRequest' } headers2 = { 'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate, br', 'Accept-Language': 'en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7', 'Connection': 'keep-alive', 'Content-Length': '112', 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', 'Cookie': 'ASP.NET_SessionId=gwqimjpsnpemccguu4ns3d0d; CookieId=gwqimjpsnpemccguu4ns3d0d; QINGCLOUDELB=0c115dd3e70db1dd010b1763523580a8eb34b25dd41eaed32dbb495bb1e757e5', 'DNT': '1', 'Host': 'www.pkulaw.cn', 'Origin': 'https://www.pkulaw.cn', 'Referer': 'https://www.pkulaw.cn/Case/', 'sec-ch-ua': '"Google Chrome 79"', 'Sec-Fetch-Dest': 'empty', 'Sec-Fetch-Mode': 'cors', 'Sec-Fetch-Site': 'same-origin', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36', 'X-Requested-With': 'XMLHttpRequest' } try: response = requests.Session( ) res = response.post(url = url1, data = data1, headers = headers1, timeout = 10) cookies1 = res.cookies.get_dict( ) cookieId = cookies1.get('CookieId') print('CookieId: ' + string(cookieId)) cookieID = cookieId print('firstlogcookie: ' + string(cookies1)) with open('./Cookies/firstlogCookie.txt', 'w', encoding = 'utf-8') as f: for key, value in cookies1.items( ): f.write(key + "=" + string(value) + "; ") f.close( ) with open('./Cookies/firstlogCookie.txt', 'rb+') as f1: f1.seek(-2, os.SEEK_END) f1.truncate( ) f1.close( ) except Exception as e1: print("Error1: " + string(e1)) pass try: with open('./Cookies/firstlogCookie.txt', 'r', encoding = 'utf-8') as f2: cookies2 = f2.readline( ) print("cookies2: " + string(cookies2)) print("headers2: " + string(headers2)) headers2.update(Cookie = cookies2) print("headers2: " + string(headers2)) print("data2: " + string(data2)) data2.update(CookieId = cookieID) print("data2: " + string(data2)) f2.close( ) response1 = requests.Session( ) res1 = response1.post(url = url1, data = data2, headers = headers2, timeout = 10) return cookies2 except Exception as e2: print("error2: " + string(e2)) pass
def singeldownload(self, name): global proxy try: print("Requesting Pages...") self.headers3.update(Cookie=self.cookie) print('headers3.getcookie: ' + string(self.headers3.get('Cookie'))) self.data3.update(gid=self.gid) proxy = self.get_proxy() proxies = {'http': 'http://' + proxy} ses = requests.Session() r = ses.head(self.url2) total = int(r.headers['Content-Length']) print(total) # print(r.status_code) while r.status_code != 500: # with ThreadPoolExecutor(max_workers = 30) as executor: # executor.map(self.download, ) thread_list = [] # 一个数字,用来标记打印每个线程 n = 0 for ran in self.get_range(total): start, end = ran # 打印信息 print('thread %d start:%s,end:%s' % (n, start, end)) n += 1 # 创建线程 传参,处理函数为download thread = threading.Thread(target=self.download( name, start, end, self.headers3, ses, self.url2, self.data3, proxies), args=(start, end)) # 启动 thread.start() thread_list.append(thread) for i in thread_list: # 设置等待 i.join() print('download %s load success' % name) # with open('./download/' + name + '.txt', 'wb') as f4: # for ran in get_range(total): # headers4['Range'] = 'Bytes=%s-%s' % ran # r = ses.get(url = url1, headers = headers4, data = data1, stream = True, proxies = proxies) # f4.seek(ran[0]) # f4.write(r.content) # f4.close( ) # res = ses.get(url = url1, headers = headers4, data = data1, stream = True, proxies = proxies) # # print('Using proxy : ' + proxy) # print(res.status_code) # while res.status_code == 200: # with open('./download/'+name+'.txt', 'wb') as f4: # for chunk in res.iter_content(chunk_size = 32): # chunk_size #设置每次下载文件的大小 # f4.write(chunk) # 每一次循环存储一次下载下来的内容 with open('./download/' + name + '.txt', 'r', encoding='GBK') as f5: lines = f5.readlines() first_line = lines[0] key = "尚未登录" if key in first_line: print(first_line + "请先登录获取cookie") return False else: print('您的账号已经登陆') return True else: print("unable to download...") return False except Exception as e: print(e) return False
if event.type == pygame.MOUSEMOTION: pygame.draw.rect(screen, BLACK, (0, 0, width, SQUARE_SIZE)) posx = event.pos[0] if turn == 0: # something is wrong up here pygame.draw.circle(screen, YELLOW, (posx, int(SQUARE_SIZE / 2)), radius) elif turn == 1: pygame.draw.circle(screen, RED, int(posx, int(SQUARE_SIZE / 2)), radius) pygame.display.update() if event.type == pygame.MOUSEBUTTONDOWN: print(event.pos) # player one if turn == 0: player = string(1) # casted to string bc I can't concatenate string and int inside of user input call # player two else: player = string(2) turn = changeTurn(turn) posx = event.pos[0] col = int(math.floor(posx / SQUARE_SIZE)) if validLoc(board, col): row = nextOpenRow(board, col) dropPiece(board, row, col, player) if checkWin(board): print("Congrats Player " + player + "! YOU WON!") gameEnd = True