Python使用爬虫抓取美女图片并保存到本地的方法【测试可用】
作者:c无常 时间:2023-11-04 19:44:17
本文实例讲述了Python使用爬虫抓取美女图片并保存到本地的方法。分享给大家供大家参考,具体如下:
图片资源来自于www.qiubaichengren.com
代码基于Python 3.5.2
友情提醒:血气方刚的骚年。请
谨慎阅图!
谨慎阅图!!
谨慎阅图!!!
code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import urllib
import urllib.request
import re
from urllib.error import URLError
class QsSpider:
def __init__(self):
self.user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
self.header = {'User-Agent': self.user_agent}
self.save_dir = './pic'
self.url = 'http://www.qiubaichengren.com/%s.html'
def start(self):
for i in range(1, 10):
self.load_html(str(i))
def load_html(self, page):
try:
web_path = self.url % page
request = urllib.request.Request(web_path, headers=self.header)
with urllib.request.urlopen(request) as f:
html_content = f.read().decode('gb2312')
# print(html_content)
self.pick_pic(html_content)
except URLError as e:
print(e.reason)
return
def save_pic(self, img):
print(img)
save_path = self.save_dir + "/" + img.replace(':', '@').replace('/', '_')
if not os.path.exists(self.save_dir):
os.makedirs(self.save_dir)
print(save_path)
urllib.request.urlretrieve(img, save_path)
pass
def pick_pic(self, html_content):
regex = r'src="(http:.*?\.(?:jpg|png|gif))'
patten = re.compile(regex)
pic_path_list = patten.findall(html_content)
for i in pic_path_list:
self.save_pic(str(i))
print(i)
spider = QsSpider()
spider.start()
代码运行后可得到如下N多大饱眼福的美女图:
希望本文所述对大家Python程序设计有所帮助。
来源:https://blog.csdn.net/cch1024/article/details/54601106
标签:Python,爬虫,抓取,美女图片
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SQL Server手工插入标识列的方法
2024-01-27 05:41:35
node.js+Ajax实现获取HTTP服务器返回数据
2024-05-13 09:29:07
![](https://img.aspxhome.com/file/2023/1/125731_0s.png)
python的re模块使用方法详解
2022-08-10 16:18:20
Python Sql数据库增删改查操作简单封装
2024-01-22 14:47:37
javascript 文档的编码问题解决
2024-04-22 22:45:22
一文详解Python中itertools模块的使用方法
2022-11-11 06:12:50
python selenium 对浏览器标签页进行关闭和切换的方法
2023-06-18 19:42:26
Python爬虫简单运用爬取代理IP的实现
2021-08-25 23:26:42
![](https://img.aspxhome.com/file/2023/1/75201_0s.jpg)
JavaScript常用的返回,自动跳转,刷新,关闭语句汇总
2023-10-21 10:37:27
php简单获取复选框值的方法
2023-11-15 20:57:28
Python ndarray 数组的变形详情
2023-05-13 14:58:47
![](https://img.aspxhome.com/file/2023/9/86239_0s.png)
python脚本框架webpy入门安装及应用创建
2023-08-23 05:08:51
一种弹出提示信息时页面背景色调改变的方法
2008-12-01 12:22:00
python 统计list中各个元素出现的次数的几种方法
2022-12-09 10:04:01
mysql使用source 命令乱码问题解决方法
2024-01-13 13:11:16
python爬虫爬取快手视频多线程下载功能
2021-10-29 09:24:04
![](https://img.aspxhome.com/file/2023/6/120426_0s.jpg)
把CSV文件导入到SQL Server表中的方法
2024-01-20 17:22:13
![](https://img.aspxhome.com/file/2023/9/99999_0s.png)
Apache SkyWalking 监控 MySQL Server 实战解析
2024-01-24 01:31:54
![](https://img.aspxhome.com/file/2023/0/115360_0s.png)
js页面文字选中后分享到新浪微博实现
2011-03-17 12:55:00
pygame学习笔记(4):声音控制
2021-08-15 20:04:35