python爬虫抓取网站相册图片(request+BeautifulSoup)

这是之前学习爬虫时鼓捣的第一个小玩意。虽然简单,但是在写的过程中还是遇到了一些小问题,特此整理一下。

爬取网站:

https://www.meituri.com

爬虫思路:

  1. 请求指定模特的首页,解析并获取该模特所有相册的链接,存放于list中。
  2. 获取模特名字以及相册总数,本地建立同名文件夹(模特名+相册数目)
  3. list中取出各相册链接,解析网页并建立相册名同名文件夹
  4. 下载相册图片到指定文件夹

完成代码:

getAlbums.py

import requests
from bs4 import BeautifulSoup
import get_one_album as goa
import os
headers = {
   
    'User-Agent' :'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
    'Referer': 'https://www.meituri.com/t/4074/'
}

# url = 'https://www.meituri.com/t/4074/'
# url_list = ['https://www.meituri.com/t/4074/']

url = 'https://www.meituri.com/t/2441/'
url_list = ['https://www.meituri.com/t/2441/']

#url = 'https://www.meituri.com/t/646/'
#url_list = ['https://www.meituri.com/t/646/']

# url = 'https://www.meituri.com/t/296/'  #有第二页
# url_list = ['https://www.meituri.com/t/296/']

os_path = 'D://爬虫/'
album_url_list = [] #各相册链接

def parse_url(url):
    html = requests.get(url,headers=headers).content
    bsObj = BeautifulSoup(html,'lxml')
    name = bsObj.find('h1').text
    return bsObj,name

def next_page(bsobj): #判断是否有多页,返回页面数量。相册多于40套时多页
    num = 1
    nextPage = bsobj.find('div', {
   'id': 'pages'})
    if nextPage:
        pagenum = nextPage.findAll('a')
        num = len(pagenum)-1
        print(pagenum)
        print('有{}页'.format(str(num)))
    else:
        print('只有1页'.format(str(num)))
    
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值