吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1461|回复: 26
收起左侧

[Python 原创] 爬虫爬取B站文章图片

  [复制链接]
kuank 发表于 2024-1-13 22:44
废话不说,上代码
import requests
import os
from bs4 import BeautifulSoup

def download_images(url):
    # 发送HTTP请求获取网页源代码
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36'
    }
    response = requests.get(url, headers=headers)
    html = response.text

    # 使用BeautifulSoup解析HTML内容
    soup = BeautifulSoup(html, 'html.parser')

    # 创建一个目录用于保存图片
    os.makedirs('bilibili_images', exist_ok=True)

    # 找到所有图片的标签
    img_tags = soup.find_all('img')

    # 遍历所有图片标签,下载图片
    for img in img_tags:
        img_url = img.get('data-src') or img.get('src')
        if img_url.startswith('//'):
            img_url = 'https:' + img_url

        # 下载图片
        response = requests.get(img_url)
        # 提取图片文件名
        img_file = img_url.split('/')[-1]
        # 将图片保存到指定目录下
        with open('bilibili_images/' + img_file, 'wb') as f:
            f.write(response.content)
            print(f"Downloaded: {img_file}")

# 要爬取的文章URL
article_url = 'https://www.bilibili.com/read/cv18302467/'
download_images(article_url)

https://wwqu.lanzouq.com/i4OyO1l13yij
密码:52pj

免费评分

参与人数 6吾爱币 +4 热心值 +6 收起 理由
dickchlan + 1 谢谢@Thanks!
luozi1653 + 1 + 1 热心回复!
soughing + 1 + 1 我很赞同!
红尘旧梦i + 1 + 1 老婆是越来越多了,我感觉三妻四妾已经满足不了我了{:301_988:}
撒旦の恶 + 1 吾爱破解论坛有你更精彩!
qq63 + 1 + 1 谢谢@Thanks!

查看全部评分

本帖被以下淘专辑推荐:

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| kuank 发表于 2024-1-15 22:50
注意:使用前要安装BeautifulSoup和requests库
williamipod 发表于 2024-1-17 14:16
During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/workspace/1/main.py", line 1, in <module>
    import requests
  File "/usr/local/lib/python3.10/site-packages/requests-2.31.0-py3.10.egg/requests/__init__.py", line 45, in <module>
    from .exceptions import RequestsDependencyWarning
  File "/usr/local/lib/python3.10/site-packages/requests-2.31.0-py3.10.egg/requests/exceptions.py", line 9, in <module>
    from .compat import JSONDecodeError as CompatJSONDecodeError
  File "/usr/local/lib/python3.10/site-packages/requests-2.31.0-py3.10.egg/requests/compat.py", line 13, in <module>
    import charset_normalizer as chardet
ModuleNotFoundError: No module named 'charset_normalizer'
出现这个这咋回事
三滑稽甲苯 发表于 2024-1-14 08:58
52bojie 发表于 2024-1-14 09:28
挺不错的,加油!
Wryyy6 发表于 2024-1-14 10:40
好用,感谢分享
sai609 发表于 2024-1-14 10:41
微信批量下载原创标签文章,python如何实现
红尘旧梦i 发表于 2024-1-14 11:08
老婆是越来越多了,我感觉三妻四妾已经满足不了我了
yiliber 发表于 2024-1-14 11:41
加油!加油!
backaxe 发表于 2024-1-14 13:33
学习一下,感谢分享思路
w360 发表于 2024-1-14 13:33
这个不会用a
AA082 发表于 2024-1-14 14:34
非常有用,感谢
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-28 11:34

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表