Crawl app photos with python

Posted by djcubez on Mon, 20 Jul 2020 16:59:48 +0200

Download a fighting fish first (you can do it without downloading, the URLs are all here, right)

Grab a json package by grabbing it and get the address below

 

Observed tests show that modifying offset values is equivalent to paging an app

Accessing this url returns a large dictionary with two indexes, one error and one data.Data is another 20-length array, and each array is a dictionary.There is another index in each dictionary, vertical_src.

Our goal is it!

 1 import urllib.parse
 2 import urllib
 3 import json
 4 import urllib.request
 5 data_info={}
 6 data_info['type']='AUTO'
 7 data_info['doctype']='json'
 8 data_info['xmlVersion']='1.6'
 9 data_info['ue']='UTF-8'
10 data_info['typoResult']='true'
11 head_info={}
12 head_info['User-Agent']='DYZB/2.271 (iphone; iOS 9.3.2; Scale/3.00)'
13 url='http://capi.douyucdn.cn/api/v1/getVerticalRoom?aid=ios&client_sys=ios&limit=20&offset=20'
14 data_info=urllib.parse.urlencode(data_info).encode('utf-8')
15 print(data_info)
16 requ=urllib.request.Request(url,data_info)
17 requ.add_header('Referer','http://capi.douyucdn.cn')
18 requ.add_header('User-Agent','DYZB/2.271 (iphone; iOS 9.3.2; Scale/3.00)')
19 response=urllib.request.urlopen(requ)
20 print(response)
21 html=response.read().decode('utf-8')

This is more than 20 lines of code to return the json data.Then the url address of each host photo is separated by slicing the json code.

Then get a picture of this page

 1 import json
 2 import urllib.request
 3 data_info={}
 4 data_info['type']='AUTO'
 5 data_info['doctype']='json'
 6 data_info['xmlVersion']='1.6'
 7 data_info['ue']='UTF-8'
 8 data_info['typoResult']='true'
 9 
1011 url+str(i)='http://capi.douyucdn.cn/api/v1/getVerticalRoom?aid=ios&client_sys=ios&limit=20&offset='+str(x)
12 data_info=urllib.parse.urlencode(data_info).encode('utf-8')
13 print(data_info)
14 requ=urllib.request.Request(url,data_info)
15 requ.add_header('Referer','http://capi.douyucdn.cn')
16 requ.add_header('User-Agent','DYZB/2.271 (iphone; iOS 9.3.2; Scale/3.00)')
17 response=urllib.request.urlopen(requ)
18 print(response)
19 html=response.read().decode('utf-8')
20 '''
21  print(type(dictionary))
22 print(type(dictionary[data]))
23 '''
24 dictionary=json.loads(html)
25 data_arr=dictionary["data"]
26 for i in range(0,19):
27     name=data_arr[i]["nickname"]
28     img_url=data_arr[i]["vertical_src"]
29     print(type(img_url))
30     respon_tem=urllib.request.urlopen(img_url)
31     anchor_img=respon_tem.read()
32     with open('../photos/'+name+'.jpg','wb') as f:
33         f.write(anchor_img)

Then change it to have page flipping

 1 import urllib.parse
 2 import urllib
 3 import json
 4 import urllib.request
 5 data_info={}
 6 data_info['type']='AUTO'
 7 data_info['doctype']='json'
 8 data_info['xmlVersion']='1.6'
 9 data_info['ue']='UTF-8'
10 data_info['typoResult']='true'
11 data_info=urllib.parse.urlencode(data_info).encode('utf-8')
12 
13 for x in range(0,195):
14     url='http://capi.douyucdn.cn/api/v1/getVerticalRoom?aid=ios&client_sys=ios&limit=20&offset='+str(x)
15     print(data_info)
16     requ=urllib.request.Request(url,data_info)
17     requ.add_header('Referer','http://capi.douyucdn.cn')
18     requ.add_header('User-Agent','DYZB/2.271 (iphone; iOS 9.3.2; Scale/3.00)')
19     response=urllib.request.urlopen(requ)
20     print(response)
21     html=response.read().decode('utf-8')
22     dictionary=json.loads(html)
23     data_arr=dictionary["data"]
24     for i in range(0,19):
25         name=data_arr[i]["nickname"]
26         img_url=data_arr[i]["vertical_src"]
27         print(type(img_url))
28         respon_tem=urllib.request.urlopen(img_url)
29         anchor_img=respon_tem.read()
30         with open('../photos/'+name+'.jpg','wb') as f:
31             f.write(anchor_img)

Then wait ~~

It's best to set the time, how often to crawl, or how often to change your ip.That's it

Topics: Python JSON iOS