爬虫2—用爬虫爬取壁纸(想爬多少张爬多少张)

先看效果图:

 我这个是爬了三页的壁纸60张。


上代码了。

import requests
import re
import os
from bs4 import BeautifulSoup

count=0
img_path = "./壁纸图片/"#指定保存地址
if not os.path.exists(img_path):
        os.mkdir(img_path)
headers={
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36 Edg/121.0.0.0",
"Accept":"image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8",
"Accept-Encoding":"gzip, deflate, br",
"Accept-Language":"zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6"
}
for num in range(2,5,1):
    url=f"http://www.netbian.com/index_{num}.htm"
    html=requests.get(url,headers=headers)
    html.encoding = "gbk"
    print(html.status_code)
    if html.ok:
        html = html.text
        #print(html)
        soup = BeautifulSoup(html,'html.parser')
        all_list=soup.find(class_="list")
        all_img = all_list.find_all("img")
        for img in all_img:
            src=img['src']
            print(src)
            count+=1
            myimg = requests.get(src)
            file_name = f'{img_path}图片{str(count)}.jpg'
            # 图片和音乐WB的二进制写入方式
            f = open(file_name, "wb")
            f.write(myimg.content)




看起来还挺简单的,但是我花了,一下午的时间,去看b站和自己试试。才搞完。效率好低。

上面导入了re的包,我想用re但是我不会经过简单的尝试放弃了。

简单说一下代码吧!!!

1.上面那个头,在我上一篇的爬虫,有该怎么找!!!
2.

count=0
img_path = "./壁纸图片/"#指定保存地址
if not os.path.exists(img_path):
        os.mkdir(img_path)

这里count是图片名字,img_path是有没有这样一个文件夹,来让我存储我的壁纸。if么有就新建。

3.

for num in range(2,5,1):
    url=f"http://www.netbian.com/index_{num}.htm"
    html=requests.get(url,headers=headers)
    html.encoding = "gbk"
    print(html.status_code)

这里的gbk我想写一下:
GBK和UTF-8的解码方式——这个就是为了防止乱码

这个是在知乎上找的,very good!

4.

 这个就很重要了

html = html.text
        #print(html)
        soup = BeautifulSoup(html,'html.parser')
        all_list=soup.find(class_="list")
        all_img = all_list.find_all("img")
        for img in all_img:
            src=img['src']
            print(src)

细说吧:

其中这个all_list是找到所有的包含了图片的列表:

找到之后,再找img的照片

然后找到src后面的网址。

5. 下来这个也重要哈

            count+=1
            myimg = requests.get(src)
            file_name = f'{img_path}图片{str(count)}.jpg'
            # 图片和音乐WB的二进制写入方式
            f = open(file_name, "wb")
            f.write(myimg.content)

请求访问src,然后起个名字,然后wb的写入方式,然后写入文件


到这里了,学习之路任重而道远。过几天读卡器回来了,就可以继续搞k210了加油

爬虫还是得一步一步爬

相关推荐

  1. vscode 编写爬虫王者荣耀壁纸

    2024-02-13 22:20:02       43 阅读
  2. 爬虫实战——结合多进程、线程池图片

    2024-02-13 22:20:02       38 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-13 22:20:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-13 22:20:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-13 22:20:02       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-13 22:20:02       20 阅读

热门阅读

  1. Python列表中的clear功能及用法举例

    2024-02-13 22:20:02       34 阅读
  2. 突破编程_C++_面试(基础知识(13))

    2024-02-13 22:20:02       26 阅读
  3. 读书笔记:《人件:PeopleWare》

    2024-02-13 22:20:02       32 阅读
  4. 阿里云Redis

    2024-02-13 22:20:02       31 阅读
  5. C语言:函数

    2024-02-13 22:20:02       29 阅读
  6. Dev-c++跑酷小游戏 1.0.0

    2024-02-13 22:20:02       27 阅读
  7. KY141 最大连续子序列

    2024-02-13 22:20:02       36 阅读
  8. 团队管理--程序员值班

    2024-02-13 22:20:02       37 阅读
  9. spring mvc和 spring boot 以及 spring cloud的区别

    2024-02-13 22:20:02       27 阅读
  10. iTop-4412 裸机程序(二十)- 按键中断Demo

    2024-02-13 22:20:02       32 阅读
  11. 2024.2.9

    2024.2.9

    2024-02-13 22:20:02      26 阅读
  12. 防火墙的区域隔离

    2024-02-13 22:20:02       33 阅读