千家信息网

如何用Python抓取阿里云盘资源

发表于:2025-02-07 作者:千家信息网编辑
千家信息网最后更新 2025年02月07日,这篇文章主要介绍"如何用Python抓取阿里云盘资源"的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇"如何用Python抓取阿里云盘资源"文章能帮助大家解决问题。
千家信息网最后更新 2025年02月07日如何用Python抓取阿里云盘资源

这篇文章主要介绍"如何用Python抓取阿里云盘资源"的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇"如何用Python抓取阿里云盘资源"文章能帮助大家解决问题。

网页分析

这个网站有两个搜索路线:搜索线路一和搜索线路二,本文章使用的是搜索线路二。

打开控制面板下的网络,一眼就看到一个 seach.html 的 get 请求。

上面带了好几个参数,四个关键参数:

  • page:页数,

  • keyword:搜索的关键字

  • category:文件分类,all(全部),video(视频),image(图片),doc(文档),audio(音频),zip(压缩文件),others(其他),脚本中默认写 all

  • search_model:搜索的线路

也是在控制面板中,看出这个网页跳转到阿里云盘获取真实的的链接是在标题上面的。用 bs4 解析页面上的 div(class=resource-item border-dashed-eee) 标签下的 a 标签就能得到跳转网盘的地址,解析 div 下的 p 标签获取资源日期。

抓取与解析

首先安装需要的 bs4 第三方库用于解析页面。

pip3 install bs4

下面是抓取解析网页的脚本代码,最后按日期降序排序。

import requestsfrom bs4 import BeautifulSoupimport stringword = input('请输入要搜索的资源名称:')    headers = {    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36'}result_list = []for i in range(1, 11):    print('正在搜索第 {} 页'.format(i))    params = {        'page': i,        'keyword': word,        'search_folder_or_file': 0,        'is_search_folder_content': 0,        'is_search_path_title': 0,        'category': 'all',        'file_extension': 'all',        'search_model': 0    }    response_html = requests.get('https://www.alipanso.com/search.html', headers = headers,params=params)    response_data = response_html.content.decode()       soup = BeautifulSoup(response_data, "html.parser");    divs = soup.find_all('div', class_='resource-item border-dashed-eee')        if len(divs) <= 0:        break    for div in divs[1:]:        p = div.find('p',class_='em')        if p == None:            break        download_url = 'https://www.alipanso.com/' + div.a['href']        date = p.text.strip();        name = div.a.text.strip();        result_list.append({'date':date, 'name':name, 'url':download_url})        if len(result_list) == 0:        break    result_list.sort(key=lambda k: k.get('date'),reverse=True)

示例结果:

模板

上面抓取完内容后,还需要将内容一个个复制到 google 浏览器中访问,有点太麻烦了。要是直接点击一下能访问就好了。小编在这里就用 Python 的模板方式写一个 html 文件。

模板文件小编是用 elements-ui 做的,下面是关键的代码:

    

在 python 中读取这个模板文件,并将 ${elements} 关键词替换为上面的解析结果。最后生成一个 report.html 文件。

with open("aliso.html", encoding='utf-8') as t:    template = string.Template(t.read())final_output = template.substitute(elements=result_list)with open("report.html", "w", encoding='utf-8') as output:    output.write(final_output)

示例结果:

跳转到阿里云盘界面

完整代码

aliso.html

                                  阿里云盘资源        

aliso.py

# -*- coding: UTF-8 -*-import requestsfrom bs4 import BeautifulSoupimport stringword = input('请输入要搜索的资源名称:')    headers = {    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36'}result_list = []for i in range(1, 11):    print('正在搜索第 {} 页'.format(i))    params = {        'page': i,        'keyword': word,        'search_folder_or_file': 0,        'is_search_folder_content': 0,        'is_search_path_title': 0,        'category': 'all',        'file_extension': 'all',        'search_model': 2    }    response_html = requests.get('https://www.alipanso.com/search.html', headers = headers,params=params)    response_data = response_html.content.decode()       soup = BeautifulSoup(response_data, "html.parser");    divs = soup.find_all('div', class_='resource-item border-dashed-eee')        if len(divs) <= 0:        break    for div in divs[1:]:        p = div.find('p',class_='em')        if p == None:            break        download_url = 'https://www.alipanso.com/' + div.a['href']        date = p.text.strip();        name = div.a.text.strip();        result_list.append({'date':date, 'name':name, 'url':download_url})        if len(result_list) == 0:        break    result_list.sort(key=lambda k: k.get('date'),reverse=True)print(result_list)with open("aliso.html", encoding='utf-8') as t:    template = string.Template(t.read())final_output = template.substitute(elements=result_list)with open("report.html", "w", encoding='utf-8') as output:    output.write(final_output)

关于"如何用Python抓取阿里云盘资源"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注行业资讯频道,小编每天都会为大家更新不同的知识点。

0