Crawl app photos using python,

Source: Internet
Author: User

Crawl app photos using python,
First download a douyu (you can do it without downloading it. The URLs are all here, right)

Capture a json data packet and get the following address.

 

According to the test results, modifying the offset value is equivalent to page flip of the app.

Access this url and return a large dictionary with two indexes, one error and one data. Data is an array of 20 characters, and each array is a dictionary. Each dictionary has another index, 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')

In this case, over 20 lines of code can return json data. Then, the json code is sliced to obtain the url of each broadcaster's photo.

Then you can get the photo on 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, modify the settings to enable the page flip function.

 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 is best to set the time, climb once every other time, or change the ip address once every other time. That's all.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.