[python爬虫]--从煎蛋网下载图片

最近在学习python爬虫,写出来的一些爬虫记录在csdn博客里,同时备份一个放在了github上。
github地址:https://github.com/wjsaya/python_spider_learn/
本次内容:从煎蛋网下载图片


思路:

  1. 拼接出所需的页面URL。
  2. 用BS去访问和解析页面URL,获取页面内的所需图片并保存到本地并重命名。

代码:

#coding: utf-8
import time
import re
import os
import requests
from bs4 import BeautifulSoup

url = 'http://jandan.net/ooxx'
firefox = {"User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:53.0) Gecko/20100101 Firefox/53.0 FirePHP/0.7.4"}
Pg_count = input("请输入想要获取的页数(直接回车默认页码为1):")
if Pg_count == "":
    Pg_count = "1"
Pg_count=int(Pg_count)-1
#给get_html函数使用,预先+1上去,这样-1就是当前第一页。

def get_content(url):
#获取url,返回BS解析之后的content
    html = requests.get(url, headers=firefox)
    content = BeautifulSoup(html.content, "lxml")
    return content

def get_current_page(content):
#获取url,返回url中的current-comment-page(即页码)
    page_swap1 = content.find('span',class_="current-comment-page")
    page_swap2 = str(page_swap1)
    current_page = page_swap2[36:39]
    #current_page = str(content.find('span',class_="current-comment-page"))[36:39]
    #此处本来是写成了一句语句直接提取,后来想了想,万一以后想再看下,这会把自己绕晕,于是乎拆分了、、、
    return current_page

def get_img_url(content, max_page):
#获取解码后的html文件以及最大页码。获取html中的图片img_url,然后把img_url和最大页码传递给get_img函数。
    #html = BeautifulSoup(content, "lxml")
    img_li = content.find_all("a", class_="view_img_link")
    for i in img_li:
        img_url = "http:"+i["href"]
        #img_url = "http://"+i["href"].strip("/")
        get_img(img_url, max_page)
        #strip,用来删除某字符
        #并且加上http协议头

def get_img(img_url, floder):
#获取img_url和文件夹名(即当前网页页码),通过img_url下载图片并保存在floder内。
    img_name = img_url[28:]
    img_file = requests.get(img_url, headers=firefox)
    q = img_file.content
    with open ("./get/"+floder+"/"+img_name, 'wb') as img_file:
        img_file.write(q)
        print (floder+"/"+img_name+" is downloaded")

def get_html(max_page, Pg_count):
#获取最大页码数及想要下载图片的页数,循环得出每页的url地址。
    #http://jandan.net/ooxx/page-107#comments
    while Pg_count >= 0:
        pg = int(max_page)-Pg_count
        url = "http://jandan.net/ooxx/page-"+str(pg)+"#comments"
        print ("当前获取到的包含图片的页面url地址为:"+url)
        Pg_count -= 1
        content = get_content(url)
        print ("开始创建图片存放目录并获取图片链接地址......")
        if os.path.exists("./get/") == False:
            os.mkdir ("./get/")
        if os.path.exists("./get/"+str(pg)) == False:
            os.mkdir ("./get/"+str(pg))
        print ("开始下载图片,本地存放目录为:./get/"+str(pg))
        time.sleep(3)
        get_img_url(content, str(pg))

def main():
#主函数,一切的开端
    content = get_content(url)
    current_page = get_current_page(content)
    get_html(current_page, int(Pg_count))


if __name__ == '__main__':
    main()

下载过程截图:
下载过程截图

恩。。。效果图:
恩。。。效果图

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值