栏目分类:
子分类:
返回
终身学习网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
终身学习网 > IT > 软件开发 > 后端开发 > Python

urllibrequest爬虫,爬取彼岸图片

Python 更新时间:发布时间: 百科书网 趣学号

  • import urllib.request
    from bs4 import BeautifulSoup
    
    def index(page):
        if page == 1:
            url = 'https://pic.netbian.com/index.html'
        else:
            url = "https://pic.netbian.com/index_{}.html".format(page)
        # url = 'https://pic.netbian.com/index.html'
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
                          'Chrome/93.0.4577.82 Safari/537.36 '
        }
        w1 = urllib.request.Request(url=url, headers=headers)
        w2 = urllib.request.urlopen(w1).read()
        data = BeautifulSoup(w2, "html.parser")
        ul = data.find("div", class_="slist").find_all("li")
        # print(ul)
        for i in ul:
            n = i.find_all("img")
            for j in n:
                #图片地址
                src = j['src']
                #图片标题
                title = j["alt"].replace(" ", "")
                print(title)
                #拼接
                src1 = "https://pic.netbian.com/" + src
                #print(src1)
                #下载图片
                urllib.request.urlretrieve(url=src1, filename="./image/"+title+".jpg")
    
    #爬取前20页
    for j in range(1, 20):
        index(j)

这是爬取的网页链接最新4K壁纸_最新4K桌面壁纸_彼岸图网

 

图片下载成功

 

转载请注明:文章转载自 www.051e.com
本文地址:http://www.051e.com/it/293681.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 ©2023-2025 051e.com

ICP备案号:京ICP备12030808号