利用python爬取斗鱼app中照片方法实例

yipeiwu_com6年前Python爬虫

前言

没想到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)

然后就等着吧~~

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对【听图阁-专注于Python设计】的支持。

相关文章

Python实现的爬虫刷回复功能示例

Python实现的爬虫刷回复功能示例

本文实例讲述了Python实现的爬虫刷回复功能。分享给大家供大家参考,具体如下: 最近闲的无聊,就想着去看看爬虫,顺着爬虫顺利的做到了模拟登录、刷帖子等等,这里简要说一下。 使用Pyth...

Python爬取商家联系电话以及各种数据的方法

Python爬取商家联系电话以及各种数据的方法

上次学会了爬取图片,这次就想着试试爬取商家的联系电话,当然,这里纯属个人技术学习,爬取过后及时删除,不得用于其它违法用途,一切后果自负。 首先我学习时用的是114黄页数据。 下面四个是用...

PHP网页抓取之抓取百度贴吧邮箱数据代码分享

PHP网页抓取之抓取百度贴吧邮箱数据代码分享

百度贴吧大家都经常逛,去逛百度贴吧的时候,经常会看到楼主分享一些资源,要求留下邮箱,楼主才给发。 对于一个热门的帖子,留下的邮箱数量是非常多的,楼主需要一个一个的去复制那些回复的邮箱,...

Python使用scrapy爬取阳光热线问政平台过程解析

目的:爬取阳光热线问政平台问题反映每个帖子里面的标题、内容、编号和帖子url CrawlSpider版流程如下: 创建爬虫项目dongguang scrapy startproje...

Python爬虫工程师面试问题总结

注:答案一般在网上都能够找到。 1.对if __name__ == 'main'的理解陈述 2.python是如何进行内存管理的? 3.请写出一段Python代码实现删除一个lis...