用python爬取app照片

首先下载一个斗鱼(不下载也可以,url都在这了对吧)

   通过抓包,抓取到一个json的数据包,得到下面的地址

 

  观察测试可知,通过修改offset值就是相当于app的翻页

  访问这个url,返回得到的是一个大字典,字典里面两个索引,一个error,一个data。而data又是一个长度为20的数组,每个数组又是一个字典。每个字典中又有一个索引,vertical_src。

  我们的目标就是它了!

 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')

这短短20多行代码就能返回得到json数据了。然后再通过对这json代码的切片,分离得到每个主播照片的url地址。

然后得到这一页的照片

 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)

然后修改一下,让它有了翻页的功能

 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)

然后就等着吧~~

最好设置一下时间,每隔多久爬一次,或者每隔多久更换一次ip。就行了

 

posted @ 2017-12-03 14:56  newen  阅读(2953)  评论(1编辑  收藏  举报