forked from Litre-WU/businessInfo-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path工商信息查询.py
821 lines (775 loc) · 34.7 KB
/
工商信息查询.py
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
# -*- coding: utf-8 -*-
# Author: Litre WU
# E-mail: [email protected]
# Software: PyCharm
# File: 工商信息查询.py
# Time: 4月 21, 2021
import asyncio
from typing import Optional, List
from fastapi import FastAPI, Header, Cookie, Depends, BackgroundTasks
from starlette.requests import Request
from pydantic import BaseModel, Field
from fastapi.responses import JSONResponse
import aiohttp
from user_agent import generate_user_agent
from lxml import etree
import pandas as pd
import json
import time
from random import randint, sample
import os
from json import load, dump
import socket
from sys import platform
from functools import lru_cache
from loguru import logger
from boltons.cacheutils import LRI, LRU
from hashlib import md5
lri_cache = LRI(max_size=100)
lru_cache = LRU(max_size=100)
logger.add(f'{os.path.basename(__file__)[:-3]}.log', rotation='200 MB', compression='zip', enqueue=True, serialize=False, encoding='utf-8', retention='7 days')
host = socket.gethostbyname(socket.gethostname())
if platform == "win32":
asyncio.set_event_loop(asyncio.ProactorEventLoop())
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
tags_metadata = [
{
"name": "企业工商信息查询接口",
"description": "企业工商信息查询(天眼查、企查查、爱企查、国家企业公示系统)",
"externalDocs": {
"description": "More",
"url": f"http://{host}/docs",
},
},
]
contact = {
"name": "Litre",
"url": "http://121.37.209.113",
"email": "[email protected]",
}
app = FastAPI(openapi_url="/api/v1/api.json", title="企业工商信息查询接口", contact=contact, openapi_tags=tags_metadata)
# 日志
async def log(request, **kwargs):
ritems = dict(request.items())
if not kwargs: kwargs = ""
log_info = f'{ritems["client"][0]} {ritems["method"]} {ritems["path"]} {ritems["type"]}/{ritems["http_version"]} {kwargs}'
logger.info(log_info)
# 首页
@app.get("/", tags=["首页"])
async def index(request: Request, user_agent: Optional[str] = Header(None), x_token: List[str] = Header(None), ):
result = {
"code": 200,
"msg": "来了!老弟",
"result": "你看这个面它又长又宽,就像这个碗它又大又圆",
"info": {
"openapi_url": "/api/v1/openapi.json",
"ip": request.client.host,
"x-token": x_token,
"user-agent": user_agent,
"headers": dict(request.headers)
}
}
return JSONResponse(result)
class Qcc(BaseModel):
key: str = Field(..., example='哔哩哔哩')
creditCode: str = Field(..., example='统一社会信用代码(暂不使用)')
@app.post("/", tags=["企业工商信息查询接口"])
async def api(data: Qcc, request: Request, background_tasks: BackgroundTasks, x_token: List[str] = Header(None),
user_agent: Optional[str] = Header(None)):
kwargs = data.dict()
await log(request, **kwargs)
key = md5(str(kwargs).encode()).hexdigest()
if lru_cache.get(key): return lru_cache[key]
result = await query(**kwargs)
if result: lru_cache[key] = result
return JSONResponse(result)
# 公共请求函数
async def pub_req(**kwargs):
if not kwargs.get("url", ""): return None
headers = {"User-Agent": generate_user_agent()} | kwargs.get("headers", {})
try:
# aiohttp
async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=10),
connector=aiohttp.TCPConnector(ssl=False), trust_env=True) as client:
proxy_auth = aiohttp.BasicAuth(kwargs.get("proxy_user", ""), kwargs.get("proxy_pass", ""))
async with client.request(method=kwargs.get("method", "GET"), url=kwargs["url"],
params=kwargs.get("params", {}),
data=kwargs.get("data", {}), headers=headers, proxy=kwargs.get("proxy", ""),
proxy_auth=proxy_auth,
timeout=kwargs.get("timeout", 5)) as rs:
if rs.status == 200:
result = await rs.read()
return result
else:
logger.info(f"pub_req {kwargs} {rs.status} {rs.text}")
time.sleep(randint(1, 2))
retry = kwargs.get("retry", 0)
retry += 1
if retry >= 2:
return None
kwargs["retry"] = retry
return await pub_req(**kwargs)
except Exception as e:
logger.info(f"pub_req {kwargs} {e}")
time.sleep(randint(1, 2))
retry = kwargs.get("retry", 0)
retry += 1
if retry >= 2:
return None
kwargs["retry"] = retry
return await pub_req(**kwargs)
# 代理
async def get_proxy(**kwargs):
if not kwargs.get("turn", 0):
time_now = int(time.time())
if not os.path.exists('proxy.json'):
with open('proxy.json', 'w') as f:
dump([], f)
with open('proxy.json', 'r') as f:
data = json.load(f)
if data:
expire_time = int(time.mktime(time.strptime(data[0]["expire_time"], "%Y-%m-%d %H:%M:%S")))
if time_now < expire_time:
return data
# # 番茄代理
# url = 'http://x.fanqieip.com/gip'
# params = {"getType": "3","qty": "1","port": "1","time": "1","city": "0","format": "2","ss": "1","dt": "1","css": ""}
# 芝麻代理
url = 'http://webapi.http.zhimacangku.com/getip'
params = {"num": "1", "type": "2", "pro": "0", "city": "0", "yys": "0", "port": "1", "time": "1", "ts": "1",
"ys": "0", "cs": "0", "lb": "1", "sb": "0", "pb": "4", "mr": "1", "regions": ""}
try:
meta = {
"url": url,
"params": params,
}
result = await pub_req(**meta)
logger.info(result.decode())
if not result: return None
result = json.loads(result)
if result.get("data", ""):
with open('proxy.json', 'w') as f:
json.dump(result["data"], f)
return result["data"]
else:
time.sleep(randint(0, 1))
retry = kwargs.get("retry", 0)
retry += 1
if retry >= 2:
return None
kwargs["retry"] = retry
return await get_proxy(**kwargs)
except Exception as e:
logger.info(e)
retry = kwargs.get("retry", 0)
retry += 1
if retry >= 2:
return None
kwargs["retry"] = retry
return await get_proxy(**kwargs)
# IP查询
async def query_ip(**kwargs):
url = 'http://httpbin.org/get?show_env=1'
try:
meta = {
"url": url,
"proxy": kwargs.get("proxy", ""),
"proxy_user": kwargs.get("proxy_user", ""),
"proxy_pass": kwargs.get("proxy_pass", ""),
}
result = await pub_req(**meta)
if not result: return None
result = json.loads(result)
# logger.info(result)
ip = result["origin"].split()[0]
return ip
except Exception as e:
logger.info(f'query_ip {e}')
time.sleep(randint(1, 2))
retry = kwargs.get("retry", 0)
retry += 1
if retry >= 2:
return None
kwargs["retry"] = retry
return await query_ip(**kwargs)
# 查询
async def query(**kwargs):
result = await qcc(**kwargs)
result = await tyc(**kwargs) if not result else result
result = await aqc(**kwargs) if not result else result
result = await gsxt(**kwargs) if not result else result
if result:
result = {"code": 200, "msg": "OK", "result": result}
else:
retry = kwargs.get("retry", 0)
retry += 1
kwargs["retry"] = retry
if retry == 1:
# 第一次代理
proxy = await get_proxy()
if proxy:
kwargs = kwargs | {"proxy": f'http://{proxy[0]["ip"]}:{proxy[0]["port"]}'}
return await query(**kwargs)
else:
kwargs = kwargs | {"proxy": ""}
return await query(**kwargs)
if retry > 2:
return {"code": 200, "msg": "Fail", "result": None}
# 第二次更换代理
proxy = await get_proxy(**{"turn": 1})
if proxy:
kwargs = kwargs | {"proxy": f'http://{proxy[0]["ip"]}:{proxy[0]["port"]}'}
else:
kwargs = kwargs | {"proxy": ""}
return await query(**kwargs)
return result
# 天眼查
async def tyc(**kwargs):
try:
meta = {
"url": "https://m.tianyancha.com/search",
"params": {"key": kwargs.get("key", "")},
"headers": {"Referer": "https://m.tianyancha.com"},
"proxy": kwargs.get("proxy", ""),
"proxy_user": kwargs.get("proxy_user", ""),
"proxy_pass": kwargs.get("proxy_user", ""),
}
result = await pub_req(**meta)
if not result: return None
html = result.decode()
ids = etree.HTML(html).xpath('//div[@class="search-company-item"]/@onclick')
if not ids: return None
ids = [x.strip("jumpToCompany('").strip("');") for x in ids]
tasks = [asyncio.create_task(tyc_detail(**{"id": ids[i], "proxy": kwargs.get("proxy", "")})) for i in
range(len(ids))]
result = await asyncio.gather(*tasks)
return [x for x in result if x]
except Exception as e:
logger.info(f'tyc {e}')
retry = kwargs.get("retry", 0)
retry += 1
if retry >= 2:
return None
kwargs["retry"] = retry
return await tyc(**kwargs)
# 天眼查详情
async def tyc_detail(**kwargs):
_id = kwargs.get("id", "")
if not _id: return None
try:
meta = {
"url": f'https://m.tianyancha.com/company/{_id}',
"headers": {
"Referer": "https://m.tianyancha.com/search",
},
"proxy": kwargs.get("proxy", ""),
"proxy_user": kwargs.get("proxy_user", ""),
"proxy_pass": kwargs.get("proxy_pass", ""),
}
result = await pub_req(**meta)
if not result: return None
html = result.decode()
divs = etree.HTML(html).xpath('//div[@class="content"]/div[@class="divide-content"]/div')
info = [x.xpath('div//text()') for x in divs] if divs else ""
data = {}
if not info:
retry = kwargs.get("retry", 0)
retry += 1
if retry >= 2:
return None
kwargs["retry"] = retry
return await tyc_detail(**kwargs)
for x in info:
if "法定代表人" in x:
if len(x) == 2:
data[x[0]] = x[1]
else:
data[x[0]] = x[2]
elif "经营范围" in x:
data[x[0]] = x[1]
else:
if len(x) > 3:
for i in range(0, len(x), 2):
data[x[i]] = x[i + 1]
else:
data[x[0]] = x[1]
result = {
"social_credit_code": data.get("统一社会信用代码", ""),
"name_cn": etree.HTML(html).xpath('//meta[@name="tyc-wx-title"]/@content')[0],
"legal_person": data.get("法定代表人", ""),
"status": data.get("经营状态", ""),
"found_date": data.get("成立日期", ""),
"registered_capital": data.get("注册资本", ""),
"really_capital": data.get("实缴资本", ""),
"issue_date": data.get("核准日期", ""),
"organization_code": data.get("组织机构代码", ""),
"regist_code": data.get("工商注册号", ""),
"taxpayer_code": data.get("纳税人识别号", ""),
"type": data.get("企业类型", ""),
"license_start_date": data.get("营业期限", ""),
"taxpayer_crop": data.get("纳税人资质", ""),
"industry_involved": data.get("行业", ""),
"province": data.get("所属地区", ""),
"regist_office": data.get("登记机关", ""),
"staff_size": data.get("人员规模", ""),
"insured_size": data.get("参保人数", ""),
"transformer_name": data.get("曾用名", ""),
"name_en": data.get("英文名称", ""),
"imp_exp_enterprise_code": data.get("进出口企业代码", ""),
"address": data.get("注册地址", ""),
"regist_address": data.get("注册地址", ""),
"business_scope": data.get("经营范围", ""),
"email": "",
"unit_phone": "",
"fax": "",
"website": ""
}
if result.get("license_start_date", ""):
result["license_start_date"], result["license_end_date"] = result["license_start_date"].split(
"至")
else:
result["license_start_date"], result["license_end_date"] = "", ""
return result
except Exception as e:
logger.info(f'tyc_detail {e}')
retry = kwargs.get("retry", 0)
retry += 1
if retry >= 2:
return None
kwargs["retry"] = retry
return await tyc_detail(**kwargs)
# 企查查
async def qcc(**kwargs):
try:
meta = {
"url": "https://www.qcc.com/web/search",
"params": {"key": kwargs.get("key", "")},
"headers": {
"Cookie": "",
"Referer": f'https://www.qcc.com/web/search?key={kwargs.get("key", "")}'
},
"proxy": kwargs.get("proxy", ""),
"proxy_user": kwargs.get("proxy_user", ""),
"proxy_pass": kwargs.get("proxy_pass", ""),
}
result = await pub_req(**meta)
if not result: return None
html = result.decode()
content = etree.HTML(html).xpath('//script[1]/text()')
content = '{"appState' + content[0].split("appState")[1].split(";(function")[
0] if content else ""
if not content: return None
result = json.loads(content)
result = result["search"]["searchRes"].get("Result", "") if result else ""
if not result:
return None
data_list = []
for r in result:
data = {
"keyNo": r.get("KeyNo", ""),
"legal_person": r.get("OperName", "").replace("<em>", "").replace("</em>", ""),
"email": r.get("Email", ""),
"unit_phone": r.get("ContactNumber", ""), "fax": "",
"address": r.get("Address", "").replace("<em>", "").replace("</em>", ""),
"website": r.get("GW", "")
}
data_list.append(data)
tasks = [asyncio.create_task(qcc_detail(**{"data": data_list[i], "proxy": kwargs.get("proxy", "")})) for i in
range(len(data_list))]
result = await asyncio.gather(*tasks)
return [x for x in result if x]
except Exception as e:
logger.info(f'qcc {e}')
retry = kwargs.get("retry", 0)
retry += 1
if retry >= 2:
return None
kwargs["retry"] = retry
return await qcc(**kwargs)
# 企查查企业详情
async def qcc_detail(**kwargs):
data = kwargs.get("data", "")
if not data: return None
try:
meta = {
# "url": f'https://www.qcc.com/firm/{data["keyNo"]}.html',
"url": f'https://www.qcc.com/cbase/{data["keyNo"]}.html',
# "url": f'https://m.qcc.com/firm/{data["keyNo"]}.html',
"headers": {
"Connection": "close",
"Cookie": "",
# "cookie": "acw_sc__v2=6062bdefc57536ceeeb840ffcf85497a600eef9f",
"Referer": f'https://www.qcc.com/firm/{data["keyNo"]}.html'
# "Referer": f'https://m.qcc.com/firm/{data["keyNo"]}.html',
},
"proxy": kwargs.get("proxy", ""),
"proxy_user": kwargs.get("proxy_user", ""),
"proxy_pass": kwargs.get("proxy_pass", ""),
}
result = await pub_req(**meta)
if not result: return data
tables = pd.read_html(result.decode())
# logger.info(tables)
info_list = []
for t in tables[0].values.tolist():
info_list += t
info = {}
for i, x in enumerate(info_list):
if i % 2 == 0:
if "复制" in x:
continue
info[x] = info_list[i + 1].replace("复制", "").strip()
result = {
"social_credit_code": info.get("统一社会信用代码", ""),
"name_cn": info.get("企业名称", ""),
"legal_person": info.get("法定代表人", ""),
"status": info.get("登记状态", ""),
"found_date": info.get("成立日期", ""),
"registered_capital": info.get("注册资本", ""),
"really_capital": info.get("实缴资本", ""),
"issue_date": info.get("核准日期", ""),
"organization_code": info.get("组织机构代码", ""),
"regist_code": info.get("工商注册号", ""),
"taxpayer_code": info.get("纳税人识别号", ""),
"type": info.get("企业类型", ""),
"license_start_date": info.get("营业期限", ""),
"taxpayer_crop": info.get("纳税人资质", ""),
"industry_involved": info.get("所属行业", ""),
"province": info.get("所属地区", ""),
"regist_office": info.get("登记机关", ""),
"staff_size": info.get("人员规模", ""),
"insured_size": info.get("参保人数", ""),
"transformer_name": info.get("曾用名", ""),
"name_en": info.get("英文名", "").split("(")[0],
"imp_exp_enterprise_code": info.get("进出口企业代码", ""),
"regist_address": info.get("注册地址", "").split()[0],
"business_scope": info.get("经营范围", ""),
}
if result.get("license_start_date", ""):
result["license_start_date"], result["license_end_date"] = (x.strip() for x in
result["license_start_date"].split(
"至"))
else:
result["license_start_date"], result["license_end_date"] = "", ""
data.pop("keyNo")
result = result | data
# # web
# table = etree.HTML(html).xpath('//table[@class="ntable"]')[0] if etree.HTML(html).xpath(
# '//table[@class="ntable"]') else ""
# if type(table) == str:
# retry = kwargs.get("retry", 0)
# retry += 1
# if retry >= 2:
# return False
# kwargs["retry"] = retry
# return await qcc_detail(**kwargs)
# trs = table.xpath('tr')
# if not trs: return None
# tds = []
# for x in trs:
# tds += x.xpath('td[@class="tb"]')
# info = {x.xpath('text()')[0].strip(): x.xpath('following-sibling::node()/text()')[0].strip() for x
# in tds if x.xpath('following-sibling::node()/text()')}
# result = {
# "social_credit_code": info.get("统一社会信用代码", ""),
# "name_cn": info.get("企业名称", ""),
# "legal_person": info.get("法定代表人", ""),
# "status": info.get("登记状态", ""),
# "found_date": info.get("成立日期", ""),
# "registered_capital": info.get("注册资本", ""),
# "really_capital": info.get("实缴资本", ""),
# "issue_date": info.get("核准日期", ""),
# "organization_code": info.get("组织机构代码", ""),
# "regist_code": info.get("工商注册号", ""),
# "taxpayer_code": info.get("纳税人识别号", ""),
# "type": info.get("企业类型", ""),
# "license_start_date": info.get("营业期限", "").strip(),
# "taxpayer_crop": info.get("纳税人资质", ""),
# "industry_involved": info.get("所属行业", ""),
# "province": info.get("所属地区", ""),
# "regist_office": info.get("登记机关", ""),
# "staff_size": info.get("人员规模", ""),
# "insured_size": info.get("参保人数", "") if info.get("参保人数", "") else
# [span.strip() for span in table.xpath('tr/td/span/text()') if span.strip()][0],
# "transformer_name": table.xpath('tr/td/div/text()')[-1].strip() if table.xpath('tr/td/div/text()') else "",
# "name_en": info.get("英文名", ""),
# "imp_exp_enterprise_code": info.get("进出口企业代码", ""),
# "regist_address": info.get("注册地址", "") if info.get("注册地址", "") else
# table.xpath('tr/td/a[@class="text-dk"]/text()')[0],
# "business_scope": info.get("经营范围", ""),
# }
# if result.get("license_start_date", ""):
# result["license_start_date"], result["license_end_date"] = (x.strip() for x in
# result["license_start_date"].split(
# "至"))
# else:
# result["license_start_date"], result["license_end_date"] = "", ""
# result["legal_person"] = data.get("legal_person", "")
# data.pop("keyNo")
# logger.info({**data, **result})
return result
except Exception as e:
logger.info(f'qcc_detail {e} {data["keyNo"]}')
retry = kwargs.get("retry", 0)
retry += 1
if retry >= 2:
return None
kwargs["retry"] = retry
return await qcc_detail(**kwargs)
# 爱企查
async def aqc(**kwargs):
try:
meta = {
"url": "https://aiqicha.baidu.com/s",
"params": {"q": kwargs.get("key", ""), "t": "0"},
"headers": {"Cookie": "", "Referer": 'https://aiqicha.baidu.com/'},
"proxy": kwargs.get("proxy", ""),
"proxy_user": kwargs.get("proxy_user", ""),
"proxy_pass": kwargs.get("proxy_pass", ""),
}
result = await pub_req(**meta)
if not result: return None
html = result.decode()
content = etree.HTML(html).xpath('//script[1]/text()')
if content:
result = '{"sid"' + content[0].split('{"sid"')[1].split(";\n")[0]
# logger.info(result)
result = json.loads(result)
data_list = []
for r in result["result"]["resultList"]:
# if not creditCode or r["regNo"] == creditCode:
# return await aqc_detail(**{"data": {"pid": r["pid"]}})
data_list.append({"pid": r["pid"]})
tasks = [asyncio.create_task(aqc_detail(**{"data": data_list[i], "proxy": kwargs.get("proxy", "")})) for i
in
range(len(data_list))]
result = await asyncio.gather(*tasks)
return [x for x in result if x]
except Exception as e:
logger.info(f'aqc {e}')
retry = kwargs.get("retry", 0)
retry += 1
if retry >= 2:
return None
kwargs["retry"] = retry
return await aqc(**kwargs)
# 爱企查企业详情
async def aqc_detail(**kwargs):
data = kwargs.get("data", "")
if not data: return None
try:
meta = {
"url": "https://aiqicha.baidu.com/detail/basicAllDataAjax",
"params": {"pid": data["pid"]},
"headers": {
"Cookie": "",
"Referer": f'https://aiqicha.baidu.com/company_detail_{data["pid"]}',
"X-Requested-With": "XMLHttpRequest",
"Zx-Open-Url": f'https://aiqicha.baidu.com/company_detail_{data["pid"]}'
},
"proxy": kwargs.get("proxy", ""),
"proxy_user": kwargs.get("proxy_user", ""),
"proxy_pass": kwargs.get("proxy_pass", ""),
}
result = await pub_req(**meta)
if not result: return None
result = json.loads(result.decode())
result = result["data"]["basicData"] if result.get("data", "") else ""
if not result:
retry = kwargs.get("retry", 0)
retry += 1
if retry >= 2:
return None
kwargs["retry"] = retry
return await aqc_detail(**kwargs)
province = f'{result["district"].split("省")[0]}省' if "省" in result.get(
"district", "") else f'{result.get("district", "").split("市")[0]}市'
result = {
"name_cn": result.get("entName", ""),
"name_en": "",
"legal_person": result.get("legalPerson", ""),
"registered_capital": result.get("regCapital", ""),
"really_capital": result.get("realCapital", ""),
"found_date": result.get("startDate", ""),
"issue_date": result.get("annualDate", ""),
"social_credit_code": result.get("unifiedCode", ""),
"organization_code": result.get("orgNo", ""),
"regist_code": result.get("licenseNumber", ""),
"taxpayer_code": result.get("regNo", ""),
"imp_exp_enterprise_code": "",
"industry_involved": result.get("industry", ""),
"type": result.get("entType", ""),
"license_start_date": result.get("startDate", ""),
"license_end_date": result.get("openTime", "").split("至")[-1].strip(),
"regist_office": result.get("authority", ""),
"staff_size": "",
"insured_size": result["insuranceInfo"]["insuranceNum"],
"province": province,
"address": result.get("addr", ""),
"business_scope": result.get("scope", ""),
"email": result.get("email", ""),
"unit_phone": result.get("telephone", ""),
"fax": "",
"website": result.get("website", ""),
"regist_address": result.get("regAddr", ""),
"transformer_name": result["prevEntName"][0] if type(result.get("prevEntName", "")) == list else
result.get("prevEntName", ""),
"status": result.get("openStatus", ""),
}
return result
except Exception as e:
logger.info(f"aqc_detail {e}")
retry = kwargs.get("retry", 0)
retry += 1
if retry >= 2:
return None
kwargs["retry"] = retry
return await aqc_detail(**kwargs)
# 国家企业信用信息公示系统
async def gsxt(**kwargs):
try:
meta = {
"method": "POST",
"url": "https://app.gsxt.gov.cn/gsxt/corp-query-app-search-1.html",
"data": {
"conditions": '{"excep_tab":"0","ill_tab":"0","area":"0","cStatus":"0","xzxk":"0","xzcf":"0","dydj":"0"}',
"searchword": kwargs.get("key", ""), "sourceType": "W"},
"headers": {"X-Requested-With": "XMLHttpRequest"},
"proxy": kwargs.get("proxy", ""),
"proxy_user": kwargs.get("proxy_user", ""),
"proxy_pass": kwargs.get("proxy_pass", ""),
}
result = await pub_req(**meta)
if not result: return None
result = json.loads(result)
if result.get("data", ""):
data_list = []
for r in result["data"]["result"]["data"]:
# if not creditCode or r["uniscId"] == creditCode:
# return await gsxt_detail(**{"data": {"pripid": r["pripid"]}})
data_list.append({"pripid": r["pripid"]})
tasks = [asyncio.create_task(gsxt_detail(**{"data": data_list[i], "proxy": kwargs.get("proxy", "")})) for i
in
range(len(data_list))]
result = await asyncio.gather(*tasks)
return [x for x in result if x]
except Exception as e:
logger.info(f'gsxt {e}')
retry = kwargs.get("retry", 0)
retry += 1
if retry >= 2:
return None
kwargs["retry"] = retry
return await gsxt(**kwargs)
# 国家企业信用信息公示系统公司详情信息
async def gsxt_detail(**kwargs):
data = kwargs.get("data", "")
try:
meta = {
"url": f'https://app.gsxt.gov.cn/gsxt/corp-query-entprise-info-primaryinfoapp-entbaseInfo-{data["pripid"]}.html',
"params": {"nodeNum": "310000", "entType": "6150", "sourceType": "W"},
"headers": {"Referer": "https://servicewechat.com", "content-type": "application/x-www-form-urlencoded",
"Accept-Encoding": "gzip, deflate, br"},
"proxy": kwargs.get("proxy", ""),
"proxy_user": kwargs.get("proxy_user", ""),
"proxy_pass": kwargs.get("proxy_pass", ""),
}
res = await pub_req(**meta)
result = {
"name_cn": data.get("entName", "").replace("<font color=red>", "").replace("</font>", ""),
"status": data.get("corpStatusString", ""),
"regist_code": data.get("regNo", ""),
"social_credit_code": data.get("uniscId", ""),
"legal_person": data.get("legelRep", ""),
"type": data.get("entTypeString", ""),
"found_date": data.get("estDate", ""),
"regist_office": data.get("regOrg", ""),
"transformer_name": data.get("historyName", "").replace("<font color=red>", "").replace("</font>", ""),
}
if not res:
return result
res = json.loads(res.decode())
if res.get("result"):
result = {
"name_cn": res["result"]["entName"],
"name_en": "",
"legal_person": res["result"]["name"],
"registered_capital": f'{res["regCaption"]}{res["regCapCurCN"]}'.strip(),
"really_capital": "",
"found_date": res["result"]["estDate"],
"issue_date": res["result"]["apprDate"],
"social_credit_code": res["result"]["uniscId"],
"organization_code": "",
"regist_code": res["result"]["regNo"],
"taxpayer_code": "",
"imp_exp_enterprise_code": "",
"industry_involved": res["result"]["industryPhy"],
"type": res["result"]["entType_CN"],
"license_start_date": res["result"]["opFrom"],
"license_end_date": res["result"]["opTo"],
"regist_office": res["result"]["regOrg_CN"],
"staff_size": "",
"insured_size": "",
"province": res["nodeNum"],
"address": res["result"]["dom"],
"business_scope": res["result"]["opScope"],
"email": "",
"unit_phone": "",
"fax": "",
"website": "",
"regist_address": res["result"]["dom"],
"transformer_name": data.get("historyName", ""),
"status": res["result"]["regState_CN"],
}
return result
except Exception as e:
logger.info(f'gsxt_detail {e}')
retry = kwargs.get("retry", 0)
retry += 1
if retry >= 2:
return None
kwargs["retry"] = retry
return await gsxt_detail(**kwargs)
async def test():
# proxy = await get_proxy()
proxy = 'http://127.0.0.1:1080'
logger.info(proxy)
rs = await qcc(**{"key": "特变电工湖南工程有限公司", "proxy": proxy})
logger.info(rs)
# tasks = [asyncio.create_task(qcc(**{"key": "特变电工湖南工程有限公司", "proxy": proxy})) for x in range(10)]
# await asyncio.gather(*tasks)
if __name__ == '__main__':
# import uvicorn
# uvicorn.run(app)
# proxy = 'http://127.0.0.1:1080'
proxy = ''
# rs = asyncio.get_event_loop().run_until_complete(test())
# rs = asyncio.get_event_loop().run_until_complete(get_proxy())
# kwargs = {"key": "上海电气集团股份有限公司", "proxy": ""}
# kwargs = {"key": "上海宽娱数码科技有限公司", "proxy": ""}
# kwargs = {"key": "厦门臻旻建筑工程有限公司", "proxy": ""}
kwargs = {"key": "哔哩哔哩", "proxy": ""}
# kwargs = {"key": "广东携众建筑咨询服务有限公司", "proxy": ""}
# kwargs = {"key": "上海茗昊机械工程有限公司", "proxy": ""}
# kwargs = {**kwargs, **sample(rs, 1)[0]}
# rs = asyncio.get_event_loop().run_until_complete(query_ip(**kwargs))
# rs = asyncio.get_event_loop().run_until_complete(tyc(**kwargs))
rs = asyncio.get_event_loop().run_until_complete(tyc_detail(**{"id": "3149889182"}))
# rs = asyncio.get_event_loop().run_until_complete(qcc(**kwargs))
# rs = asyncio.get_event_loop().run_until_complete(
# qcc_detail(**{"data": {"keyNo": "hbdc8d27a2a556cfcac5001e38f41061"}}))
# rs = asyncio.get_event_loop().run_until_complete(
# qcc_detail(**{"data": {"keyNo": "963f4179841540334d3a16db3fc3567d"}}))
# rs = asyncio.get_event_loop().run_until_complete(get_proxy(**{"turn": 1}))
# rs = asyncio.get_event_loop().run_until_complete(
# qcc_detail(**{"url": "https://www.qcc.com/firm/963f4179841540334d3a16db3fc3567d.html"}))
# rs = asyncio.get_event_loop().run_until_complete(aqc(**kwargs))
# rs = asyncio.get_event_loop().run_until_complete(aqc_detail(**{"data": {"pid": "43880125442188"}}))
# rs = asyncio.get_event_loop().run_until_complete(gsxt(**kwargs))
# pripid = "D1FDF711DFE03EE312CC2ACD3CE218AB448EC78EC78E61ABE228E2ABE2ABE2ABEEABE2ABDF960DC782CB82C7647C-1618992356543"
# pripid = 'AF2B89C7A13640356C1A541B4234667D3A58B958B9581F7D9C7D9C7D9C7D9C7D1FF213F2F9185FBEDC3DDC3D5F18-1629364295083'
# rs = asyncio.get_event_loop().run_until_complete(gsxt_detail(**{"data": {"pripid": pripid}}))
# rs = asyncio.get_event_loop().run_until_complete(get_proxy())
# rs = asyncio.get_event_loop().run_until_complete(query_ip(**{"proxy": "http://182.111.108.203:45113"}))
logger.info(rs)
# Tunnel connection failed: 401 Authorized failed