python爬虫之爬取图片(一)

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Created by Fzy on 2019/1/22 13:35
import requests
import re
from bs4 import BeautifulSoup
url = 'https://www.537uu.com/meinv/26089.html'
headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) \
    Chrome/70.0.3538.102 Safari/537.36'
}
r = requests.get(url, headers=headers)
r.encoding = 'utf-8'
# print(r.text)
html = r.text
# 方法一
us = re.findall(r'data-original="(.*?)"', html, re.S)
# print(us)
for u in us:
    # print(u)
    ur = u.split('/')
    # print(ur[-1])
    filename = ur[-1]
    res = requests.get(u)
    with open(filename, 'wb') as file:
        file.write(res.content)
    print(filename+' 已下载完成!')


# 方法二
# soup = BeautifulSoup(html, 'lxml')
# us = soup.find_all('img', class_="videopic lazy")
# for u in us:
#     u = u.get('data-original')
#     # print(u)
#     ur = u.split('/')
#     # print(ur[-1])
#     filename = ur[-1]
#     res = requests.get(u)
#     with open(filename, 'wb') as file:
#         file.write(res.content)
#     print(filename+' 已下载完成!')
上一篇:【VS开发】循序渐进学习使用WINPCAP(一)


下一篇:spring boot+vue实现爬取各大平台每日热榜数据功能