目标地址
- http://www.gsxt.gov.cn/corp-query-entprise-info-xxgg-100000.html
分析
-
POST
-
http://www.gsxt.gov.cn/affiche-query-area-info-paperall.html?noticeType=21&areaid=100000¬iceTitle=®Org=110000
-
参数:
-
请求体数据:
- draw: 1 点击分页按钮的次数, 可以省略
- start: 0 起始索引号
- length: 10 长度, 固定是10, 多设置也无效
直接访问接口时,状态521,返回如下js混淆加密代码。
def get_content(): url = 'http://www.gsxt.gov.cn/corp-query-entprise-info-xxgg-100000.html' r = requests.post(url) print(r.content.decode()) print(r.status_code) get_content()
<script>document.cookie=('_') ('_') ('j') ('s') ('l') ('_') ('c') ('l') ('e') ('a') ('r') ('a') ('n') ('c') ('e') ('=')+(-~false+'')+(3+3+'')+((1<<2)+'')+((1+[2])/[2]+'')+(+!+[]+'')+((1+[2]>>2)+'')+(1+3+'')+((2)*[2]+'')+(-~(3)+'')+([3]*(3)+'')+('.')+(([2]+0>>2)+'')+(7+'')+(-~[7]+'')+('|')+('-')+(-~false+'')+('|')+('s')+('g')+('w')+('T')+('p')+('u')+('n')+('A')+('E')+('%')+(2+'')+('B')+('H')+('D')+(1+4+'')+(~~{
}+'')+('h')+('O')+(-~false+'')+('Z')+('V')+('C')+('%')+((1<<1)+'')+('B')+('q')+('v')+('T')+('S')+('F')+('A')+('%')+(1+2+'')+('D')+(';')+('m')+('a')+('x')+('-')+('a')+('g')+('e')+('=')+((1+[2]>>2)+'')+(1+5+'')+(~~false+'')+(~~''+'')+(';')+('p')+('a')+('t')+('h')+('=')+('/');location.href=location.pathname+location.search</script>
521
尝试给请求头中加上Cookie
Cookie: __jsluid_h=d68888fc6e931f40e4508787817b806a; SECTOKEN=7333700017634149567; JSESSIONID=ed7a3f472552e3d96c432bfab4b5; tlb_cookie=S172.16.12.114; td_cookie=1577165912; __jsl_clearance=1646134602.818|0|bZ57isjV6P6ripFigR4CZ8zjj7U%3D
正常返回如下结果。
def get_content():
url = 'http://www.gsxt.gov.cn/affiche-query-area-info-paperall.html?noticeType=21&areaid=100000¬iceTitle=®Org=110000'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36',
'Cookie': '__jsluid_h=d68888fc6e931f40e4508787817b806a; SECTOKEN=7333700017634149567; JSESSIONID=ed7a3f472552e3d96c432bfab4b5; tlb_cookie=S172.16.12.114; td_cookie=1577165912; __jsl_clearance=1646134602.818|0|bZ57isjV6P6ripFigR4CZ8zjj7U%3D'
}
r = requests.post(url,headers = headers)
print(r.content.decode())
print(r.status_code)
get_content()
抓包分析
打开fiddler,刷新页面抓包 http://www.gsxt.gov.cn/corp-query-entprise-info-xxgg-100000.html 一共访问了三次该页面,前两次521,第三次正场返回请求页面。
第一次请求
第一次请求时服务端向服务器塞入第一个__jsluid_h关键cookie
Set-Cookie: __jsluid_h=01737b0139a221e260d1061c68b4232f; max-age=31536000; path=/; HttpOnly
并向浏览器返回一段混淆过的js,内容如下,主要功能应该是设置cookie。
<script>document.cookie=('_')+('_')+('j')+('s')+('l')+('_')+('c')+('l')+('e')+('a')+('r')+('a')+('n')+('c')+('e')+('=')+((+true)+'')+((1+[2])/[2]+'')+(-~false+'')+(3+6+'')+(2+2+'')+(-~[2]+'')+(~~false+'')+((2)*[4]+'')+((1+[0])/[2]+'')+(9-1*2+'')+('.')+((2)*[2]+'')+(6+'')+(1+6+'')+('|')+('-')+(-~0+'')+('|')+('w')+('Y')+('p')+('A')+('F')+('R')+('%')+(1+1+'')+('B')+(-~(4)+'')+('X')+('x')+('m')+('W')+('Y')+((1<<2)+'')+('j')+('P')+('a')+('Q')+([3]*(3)+'')+('t')+('Q')+(1+7+'')+('T')+('P')+('Z')+('i')+('E')+('%')+(3+'')+('D')+(';')+('m')+('a')+('x')+('-')+('a')+('g')+('e')+('=')+(3+'')+(3+3+'')+(~~''+'')+(~~false+'')+(';')+('p')+('a')+('t')+('h')+('=')+('/');location.href=location.pathname+location.search</script>
我们将这段js放入浏览器中执行,如我们所料,正是返回第二个__jsl_clearance关键性cookie。
第二次请求
第二次请求http://www.gsxt.gov.cn/corp-query-entprise-info-xxgg-100000.html时,浏览器发送了__jsl_clearance和__jsluid_h两个关键cookie,服务端接收后并返回了一段混淆后的js,这次没有向浏览器塞入cookie。
Cookie: __jsluid_h=01737b0139a221e260d1061c68b4232f; __jsl_clearance=1619430857.467|-1|wYpAFR%2B5XxmWY4jPaQ9tQ8TPZiE%3D
第三次请求
第三次请求http://www.gsxt.gov.cn/corp-query-entprise-info-xxgg-100000.html时,浏览器携带了两个关键性Cookie,不过这次__jsl_clearance和之前的值发生变化,怀疑是第二次请求返回的js做了手脚。
Cookie: __jsluid_h=01737b0139a221e260d1061c68b4232f; __jsl_clearance=1619430857.529|0|WGn9gKxiPtYUHAGBZ1a%2B%2F3o9sJE%3D
这次请求不仅正常的返回了页面内容,而且还往请求头中塞入了第三个SECTOKEN关键性Cookie。
Set-Cookie: SECTOKEN=7448433543385710812; Expires=Sat, 14-May-2089 13:03:34 GMT; Path=/; HttpOnly
请求数据
在上面的三个cookie加持下,终于成功的返回了我们想要的数据,接下来就是分析这三个cookie在代码中如何拿到。
爬虫实现
通过requests的session对象,自动合并cookie信息,并设置请求头。
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36',
}
session = requests.session()
session.headers = headers
第一次请求
通过第一次请求,服务端往客户端返回__jsluid_h作为第一个关键cookie,根据返回的js通过正则表达式拿到script标签中的js逻辑,并去除后面无用jslocation.href...
index_url = 'http://www.gsxt.gov.cn/corp-query-entprise-info-xxgg-100000.html'
response = session.get(index_url)
# <script>document.cookie=('_')+('_')+('j')+('s')+('l')+('_')+('c')+('l')+('e')+('a')+('r')+('a')+('n')+('c')+('e')+('=')+(-~0+'')+(1+5+'')+(-~(3)+'')+([2]*(3)+'')+(-~{}+'')+(-~[]+'')+(1+8+'')+(1+2+'')+(1+6+'')+((1<<3)+'')+('.')+(-~1+'')+((2)*[2]+'')+('|')+('-')+(-~0+'')+('|')+(+!+[]*2+'')+('K')+(~~[]+'')+(~~''+'')+('x')+(1+8+'')+(1+[0]-(1)+'')+('t')+('K')+('k')+('s')+('y')+('I')+('D')+('m')+('r')+('r')+('a')+('n')+((2^1)+'')+('Z')+('d')+(1+8+'')+('h')+('j')+('k')+('Y')+('%')+(3+'')+('D')+(';')+('m')+('a')+('x')+('-')+('a')+('g')+('e')+('=')+((1+[2]>>2)+'')+([2]*(3)+'')+(~~false+'')+(~~''+'')+(';')+('p')+('a')+('t')+('h')+('=')+('/');location.href=location.pathname+location.search</script>
print(session.get(index_url).content.decode(('utf-8')))
js1 = re.findall('<script>(.+?)</script>', response.content.decode())[0].replace('document.cookie=', '').replace('location.href=location.pathname+location.search', '')
context = js2py.EvalJs()
context.execute('cookies2 =' + js1)
cookies = context.cookies2.split(';')[0].split('=')
session.cookies.set(cookies[0], cookies[1]) # 到此拿到第两个cookie
print(cookies)
print(session.cookies)
print("-----------------------------------------------------------------")
<script>document.cookie=('_')+('_')+('j')+('s')+('l')+('_')+('c')+('l')+('e')+('a')+('r')+('a')+('n')+('c')+('e')+('=')+((+true)+'')+(2+4+'')+(-~(3)+'')+(2+4+'')+(-~0+'')+(-~[2]+'')+((1+[2])/[2]+'')+((2<<1)+'')+(([2]+0>>2)+'')+((1+[2]>>2)+'')+('.')+(9-1*2+'')+((2)*[2]+'')+(+!+[]*2+'')+('|')+('-')+(-~0+'')+('|')+('N')+('F')+('T')+('K')+('e')+('K')+('y')+('p')+('M')+('n')+('I')+('Z')+('v')+('a')+('W') 标签:
三极管sje13003