python爬虫(八、爬取图片社的小姐姐图片并下载)

爬取网页

. , \color{Red}Ⅰ.先抓取下这个网页,套模板就好了

def ask(url):
    head = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36"}
    re = urllib.request.Request(url=url,headers=head)
    res = urllib.request.urlopen(re)
    html = res.read().decode('utf-8')
    return html

. u r l \color{orange}Ⅱ.抓取图片的url

, < d i v    c l a s s = l i s t > , 如图,图片保存在<div\ \ class='list'>中,所以把这部分抓取下来

d a t a o r i g i n a l 然后发现这个data-original就是图片的地址


findload = re.compile('<img.*data-original="(.*?)"')

def download_img(baseurl):
    html = ask(baseurl)
    soup = BeautifulSoup(html,'html.parser')
    k=0
    for item in soup.find_all('div',class_='list'):
        item = str(item)
        tupian = re.findall(findload,item)[0]
        url = "http:"+tupian	#注意图片路径加上http:
        name = "D:\\妹子图片\\"	#路径
        name = name + str(k) + ".jpg"
        k+=1
        img = urllib.request.urlopen(url)	#打开图片
        f = open(name, 'wb')	#打开本地路径
        f.write(img.read())	#写进去
        f.close

. Ⅲ.主函数

import re
import xlwt
import urllib.request,parser
from bs4 import BeautifulSoup


def ask(url):
    head = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.70 Safari/537.36"}
    re = urllib.request.Request(url=url,headers=head)
    res = urllib.request.urlopen(re)
    html = res.read().decode('utf-8')
    return html

def download_img(baseurl):
    html = ask(baseurl)
    soup = BeautifulSoup(html,'html.parser')
    k=0
    for item in soup.find_all('div',class_='list'):
        item = str(item)
        tupian = re.findall(findload,item)[0]
        url = "http:"+tupian
        name = "D:\\妹子图片\\"
        name = name + str(k) + ".jpg"
        k+=1
        img = urllib.request.urlopen(url)
        f = open(name, 'wb')
        f.write(img.read())
        f.close


findload = re.compile('<img.*data-original="(.*?)"')
url = "http://699pic.com/tupian/xiaojiejie.html"
download_img(url)

\color{green}效果如图

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/jziwjxjd/article/details/106864267
今日推荐