1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
| import requests import http.cookiejar as cookielib
import urllib
import time import re from bs4 import BeautifulSoup
from aip import AipOcr
from PIL import Image from io import BytesIO
session = requests.Session()
session.cookies = cookielib.LWPCookieJar(filename="cookies.txt")
def verify_result_flow_baidugushici(inputpare = '故西辞黄鹤楼'): '''
:param inputpare: 输入古诗词除了缺失待验证的汉字外的其他汉字词组 :return:返回这句古诗词中确实待验证的验证汉字 ''' headers = { 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3', 'Accept-Encoding': 'gzip, deflate, br', 'Accept-Language': 'zh-CN,zh;q=0.9', 'Cache-Control': 'max-age=0', 'Connection': 'keep-alive', 'Cookie': 'BAIDUID=3962E06AB8A904AC40FD84F3E2A70D90:FG=1; BIDUPSID=3962E06AB8A904AC40FD84F3E2A70D90; PSTM=1562918639; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; Hm_lvt_010e9ef9290225e88b64ebf20166c8c4=1563587125; BDUSS=p-LTh2WUpIbGJtVzR4TXJpemVVOFBUQTJTTzl5TW1wNXFMN2tvaHcwd1dVVnBkSUFBQUFBJCQAAAAAAAAAAAEAAADbXiAjcGFuY2hhb3BhbgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABbEMl0WxDJdQW; H_PS_PSSID=1434_21120_29523_29520_28518_29098_28832_29221_26350_29458; Hm_lpvt_010e9ef9290225e88b64ebf20166c8c4=1563609689', 'Host': 'hanyu.baidu.com', 'Upgrade-Insecure-Requests': '1', 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36' } url = 'https://hanyu.baidu.com/s?wd='+ inputpare +'&from=poem' s = requests.Session() response_page = s.get(url, headers=headers) response_page.encoding = response_page.apparent_encoding
soup = BeautifulSoup(response_page.text, features='html.parser') try: try: soup_obj = soup.find("div", class_=u"poem-list-item-body check-red").text except: soup_obj = soup.find("div", class_=u"poem-detail-item-content").text print(soup_obj) except: soup_obj="" print(type(soup_obj), "输出的字符串为:", soup_obj.strip())
dealords = re.split(r'[, 。]', soup_obj.replace('.', '').strip().lstrip(',').rstrip('。')) print(dealords) similarnumber = 0 comparetext = '' outputtext = '' for i in range(0, len(dealords)): for x in dealords[i]: if x in inputpare: similarnumber += 1 if similarnumber >= 3: comparetext = dealords[i] print(len(comparetext), comparetext) break
for i in range(0, len(comparetext)): for x in comparetext[i]: if x in inputpare: similarnumber = 0 else: outputtext=x return outputtext
def get_file_content(filePath): ''' 从本地读取古诗词验证码的待识别文字的图片 :param inputpare: 本地验证码图片的保留位置 :return:返回从本地读取的图片 ''' with open(filePath, 'rb') as fp: return fp.read()
def verify_by_gushici(): '''
:return: 返回验证码中缺少的古诗词中的那个文字 ''' """ 你的 APPID AK SK """ APP_ID = '16xxxx43' API_KEY = 'IFeIxxxxxxEl' SECRET_KEY = 'S0OPhLxxxxxxxxwX'
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
headers = { 'Accept': 'image/webp,image/apng,image/*,*/*;q=0.8', 'Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'zh-CN,zh;q=0.9', 'Connection': 'keep-alive', 'Cookie': 'ASP.NET_SessionId=jwe1ekgshmlwzy2forzsubsd', 'Host': 'buy.mm.com.cn', 'Referer': 'http://buy.mm.com.cn/mimi/zhaozhao/neiyi/Default.aspx', 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36' } captcha_url = 'http://buy.mm.com.cn/mimi/zhaozhao/neiyi/image.aspx?' response = session.get(captcha_url, headers=headers, timeout=0.1)
image = Image.open(BytesIO(response.content)) image.save('./verify.jpg') image = get_file_content('verify.jpg')
""" 如果有可选参数 """ options = {} options["detect_direction"] = "true" options["probability"] = "true" try: """ 带参数调用通用文字识别(高精度版) """ Accuratemessage = client.basicAccurate(image, options)
words = Accuratemessage['words_result'][0]['words']
print("shuchu:", type(words), words)
verify_result = verify_result_flow_baidugushici(words) print(verify_result) return verify_result except: print(Accuratemessage) return '错'
def log_out(): session.cookies.clear() print("login out")
def moni_login(): ''' 模拟客户端登录操作 :return:none ''' headers = { 'Accept': 'application/json, text/javascript, */*; q=0.01', 'Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'zh-CN,zh;q=0.9', 'Connection': 'keep-alive', 'Content-Length': '56', 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', 'Cookie': 'ASP.NET_SessionId=jwe1ekgshmlwzy2forzsubsd', 'Host': 'buy.mm.com.cn', 'Origin': 'buy.mm.com.cn', 'Referer': 'http://buy.mm.com.cn/mimi/zhaozhao/neiyi/Default.aspx', 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36', 'X-Requested-With': 'XMLHttpRequest' } log_out() url = 'http://buy.mm.com.cn/mimi/zhaozhao/neiyi/login.aspx' code = verify_by_gushici() print(code) form = { 'id': '2035229347', 'pwd': 'dddddd2035229347', 'number': code, 'act': 'dlogin' }
print(type(code), "su:", form)
login_page = session.post(url, data=form, headers=headers, timeout=0.5) context = login_page.content.decode('utf-8') session.cookies.save() print(context)
def shoudongxuanzuo(): headers = { 'Accept': 'application/json, text/javascript, */*; q=0.01', 'Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'zh-CN,zh;q=0.9', 'Connection': 'keep-alive', 'Cookie': 'ASP.NET_SessionId=xjen3olvq5lbydin0rgjafqr', 'Host': 'buy.mm.com.cn', 'Referer': 'http://buy.mm.com.cn/mimi/zhaozhao/neiyi/Default.aspx', 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36', 'X-Requested-With': 'XMLHttpRequest' }
content = { 'dialogid': '', 'type': 'ts', 'prop': '' }
url = "http://buy.mm.com.cn/mimi/zhaozhao/neiyi/reserve.aspx?" url_code1 = verify_by_gushici()
print("xuanzdebug:\n%s" % url)
try: session.cookies.load(ignore_discard=True) print("Cookie正常加载") except: print("Cookie未能加载") choise_seat = session.get(url, params=content, headers=headers, timeout=0.5) context = choise_seat.content.decode('utf-8') print(context)
if __name__ == '__main__':
moni_login() shoudongxuanzuo()
|