Python实战:利用斗鱼API接口抓取主播高清图片

温以宁
温以宁
发布时间:2026-10-13 | 3人浏览过
原创

本文详细介绍如何使用Python编写脚本,通过抓包获取斗鱼App的垂直房间API接口,解析返回的JSON数据并提取主播头像链接。文章包含完整的Python代码示例,演示如何构造请求、处理翻页逻辑(offset参数)以及批量保存高清图片至本地文件夹,适合Python初学者学习网络爬虫基础。

前言

Python的强大功能令人惊叹,它让繁琐的操作变得简单。过去我们只能手动一张张复制粘贴图片,如今借助Python编程,我们可以自动化地将大量图片保存下来,极大地提升了效率。

近期注意到斗鱼平台上的部分主播照片质量较高,因此决定运用新掌握的Python技术进行抓取。本文将详细分享实现这一过程的具体步骤,供读者参考学习。

具体实施方法如下:

第一步,建议在手机上安装斗鱼App(当然,即使不安装App,只要掌握了相关URL地址,同样可以进行抓取)。

接下来,通过抓包工具捕获网络请求,定位到一个返回JSON格式数据的数据包,从而获取到关键的API地址。

 

经过观察和测试发现,通过修改URL中的offset参数值,即可实现类似App内的分页翻页功能。

访问该URL后,服务器返回一个大型字典对象。该字典主要包含两个键:error和data。其中,data键对应的值是一个长度为20的数组,数组中的每个元素又是一个字典。在这些子字典中,我们关注名为vertical_src的索引键。

这个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 相关产品和应用

全部下载

专题列表