发布于 2018-02-27 21:58:43 | 302 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的Python3 Cookbook中文版,程序狗速度看过来!

Python编程语言

Python 是一种面向对象、解释型计算机程序设计语言,由Guido van Rossum于1989年底发明,第一个公开发行版发行于1991年。Python语法简洁而清晰,具有丰富和强大的类库。它常被昵称为胶水语言,它能够把用其他语言制作的各种模块(尤其是C/C++)很轻松地联结在一起。


最近在学习python,通过实践是学习的一个好办法,下面这篇文章就来给大家介绍了关于利用python爬取斗鱼app中照片的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友下面来一起看看吧。

前言

没想到python是如此强大,令人着迷,以前看见图片总是一张一张复制粘贴,现在好了,学会python就可以用程序将一张张图片,保存下来。

最近看到斗鱼里的照片都不错,决定用最新学习的python技术进行爬取,下面将实现的过程分享出来供大家参考,下面话不多说了,来一起看看详细的介绍吧。

方法如下:

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

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

 

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

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

  我们的目标就是它了!


import urllib.parse
import urllib
import json
import urllib.request
data_info={}
data_info['type']='AUTO'
data_info['doctype']='json'
data_info['xmlVersion']='1.6'
data_info['ue']='UTF-8'
data_info['typoResult']='true'
head_info={}
head_info['User-Agent']='DYZB/2.271 (iphone; iOS 9.3.2; Scale/3.00)'
url='http://capi.douyucdn.cn/api/v1/getVerticalRoom?aid=ios&client_sys=ios&limit=20&offset=20'
data_info=urllib.parse.urlencode(data_info).encode('utf-8')
print(data_info)
requ=urllib.request.Request(url,data_info)
requ.add_header('Referer','http://capi.douyucdn.cn')
requ.add_header('User-Agent','DYZB/2.271 (iphone; iOS 9.3.2; Scale/3.00)')
response=urllib.request.urlopen(requ)
print(response)
html=response.read().decode('utf-8')

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

然后得到这一页的照片


import json
import urllib.request
data_info={}
data_info['type']='AUTO'
data_info['doctype']='json'
data_info['xmlVersion']='1.6'
data_info['ue']='UTF-8'
data_info['typoResult']='true'

url+str(i)='http://capi.douyucdn.cn/api/v1/getVerticalRoom?aid=ios&client_sys=ios&limit=20&offset='+str(x)
data_info=urllib.parse.urlencode(data_info).encode('utf-8')
print(data_info)
requ=urllib.request.Request(url,data_info)
requ.add_header('Referer','http://capi.douyucdn.cn')
requ.add_header('User-Agent','DYZB/2.271 (iphone; iOS 9.3.2; Scale/3.00)')
response=urllib.request.urlopen(requ)
print(response)
html=response.read().decode('utf-8')
'''
 print(type(dictionary))
print(type(dictionary[data]))
'''
dictionary=json.loads(html)
data_arr=dictionary["data"]
for i in range(0,19):
  name=data_arr[i]["nickname"]
  img_url=data_arr[i]["vertical_src"]
  print(type(img_url))
  respon_tem=urllib.request.urlopen(img_url)
  anchor_img=respon_tem.read()
  with open('../photos/'+name+'.jpg','wb') as f:
    f.write(anchor_img)

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


import urllib.parse
import urllib
import json
import urllib.request
data_info={}
data_info['type']='AUTO'
data_info['doctype']='json'
data_info['xmlVersion']='1.6'
data_info['ue']='UTF-8'
data_info['typoResult']='true'
data_info=urllib.parse.urlencode(data_info).encode('utf-8')

for x in range(0,195):
  url='http://capi.douyucdn.cn/api/v1/getVerticalRoom?aid=ios&client_sys=ios&limit=20&offset='+str(x)
  print(data_info)
  requ=urllib.request.Request(url,data_info)
  requ.add_header('Referer','http://capi.douyucdn.cn')
  requ.add_header('User-Agent','DYZB/2.271 (iphone; iOS 9.3.2; Scale/3.00)')
  response=urllib.request.urlopen(requ)
  print(response)
  html=response.read().decode('utf-8')
  dictionary=json.loads(html)
  data_arr=dictionary["data"]
  for i in range(0,19):
    name=data_arr[i]["nickname"]
    img_url=data_arr[i]["vertical_src"]
    print(type(img_url))
    respon_tem=urllib.request.urlopen(img_url)
    anchor_img=respon_tem.read()
    with open('../photos/'+name+'.jpg','wb') as f:
      f.write(anchor_img)

然后就等着吧~~

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对PHPERZ的支持。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务