代码功能介绍:
- 完成在
http://iir.circ.gov.cn/ipq/ipiQuery.html
页面的自动化查询 - 使用百度AI的开放API来识别验证码, 此处已经封装, 不建议直接复制使用.
代码如下:
import asyncio
from pyppeteer import launch
import os
import sys
import inspect
import arrow
import requests
from requests.cookies import RequestsCookieJar
from datetime import datetime
import json
import re
common_dir = os.path.realpath(os.path.abspath(os.path.join(
os.path.split(inspect.getfile(inspect.currentframe()))[0],
"../../../")))
if common_dir not in sys.path:
sys.path.insert(0, common_dir)
from ai.AIfactory.AIfactory import AIfactory
class AgentQuery():
def __init__(self, *args, **kwargs):
self.ai = AIfactory.create_ai(ai_company='baidu')(**kwargs)
self.session = requests.Session()
self.session.headers = {
"User-Agent":'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36',
}
self.jar = RequestsCookieJar()
self.host = 'http://iir.circ.gov.cn/ipq/ipiQuery.html'
self.captcha_url = 'http://iir.circ.gov.cn/ipq/captchacn.svl'
self.base_url = 'http://iir.circ.gov.cn/ipq/ipiqueryru.html'
self.baseinfo_url = 'http://iir.circ.gov.cn/ipq/ipiqueryru.do?validate'
self.detail_url = 'http://iir.circ.gov.cn/ipq/ipiqueryru.do?query'
async def main():
result = {}
obj = AgentQuery(choice='general_basic')
browser = await launch(
ignoreHTTPSErrors=True,
headless=False,
autoClose=False
)
page = await browser.newPage()
await page.setViewport(viewport={'width':1280, 'height':800})
await page.setJavaScriptEnabled(enabled=True)
await page.goto(obj.host)
await page.setUserAgent(obj.session.headers.get('User-Agent'))
await page.type('input#agencyname','世纪保险经纪股份有限公司上海分公司')
cookies = await page.cookies()
for cookie in cookies:
obj.jar.set(cookie['name'], cookie['value'])
response2 = obj.session.get(obj.captcha_url, cookies=obj.jar)
img_path = os.path.join(common_dir,'agencycaptchacn.png')
with open(img_path, 'wb') as f:
f.write(response2.content)
captcha = obj.ai.get_result(img_path)
print('识别后的验证码是', captcha)
if captcha is None or len(captcha)!=4:
await main()
else:
await page.type('input#yzm',captcha)
await page.click('#chaxun')
await page.waitFor(3000)
await page.reload()
page_list = await browser.pages()
page = page_list[-1]
if page.url == obj.base_url:
content = await page.content()
cookies = await page.cookies()
not_found_info = await page.Jx('//*[@id="interme"]/tr[1]/td')
if not_found_info:
not_found_info = await ( await not_found_info[0].getProperty('textContent')).jsonValue()
if '很抱歉,没有找到符合条件的记录' in not_found_info:
return result
agentname = await page.Jx('//*[@id="interme"]/tr[1]/td[2]/a')
if agentname:
agentname = await ( await agentname[0].getProperty('textContent')).jsonValue()
agencycode = await page.Jx('//*[@id="interme"]/tr[1]/td[3]')
if agencycode:
agencycode = await ( await agencycode[0].getProperty('textContent')).jsonValue()
regulatorycode = await page.Jx('//*[@id="interme"]/tr[1]/td[4]')
if regulatorycode:
regulatorycode = await ( await regulatorycode[0].getProperty('textContent')).jsonValue()
registertime = await page.Jx('//*[@id="interme"]/tr[1]/td[5]')
if registertime:
registertime = await ( await registertime[0].getProperty('textContent')).jsonValue()
result.setdefault('agentname', agentname)
result.setdefault('agencycode', agencycode)
result.setdefault('regulatorycode', regulatorycode)
result.setdefault('registertime', registertime)
return result
org_no = await page.Jx('//*[@id="interme"]/tr[1]/td[2]/a')
if org_no:
org_no = await ( await org_no[0].getProperty('href')).jsonValue()
org_no_list = re.findall(r"\".*\"", str(org_no))
if org_no_list:
org_no = re.sub(r'\"', '', org_no_list[0])
for cookie in cookies:
obj.jar.set(cookie['name'], cookie['value'])
response3 = obj.session.post(
obj.detail_url,
data = {
'org_no': org_no
},
cookies=obj.jar
)
print(response3.text)
if __name__=='__main__':
asyncio.get_event_loop().run_until_complete(main())