Python制作豆瓣图片的爬虫
作者:是与非 时间:2021-11-24 05:53:05
前段时间自学了一段时间的Python,想着浓一点项目来练练手。看着大佬们一说就是爬了100W+的数据就非常的羡慕,不过对于我这种初学者来说,也就爬一爬图片。
我相信很多人的第一个爬虫程序都是爬去贴吧的图片,嗯,我平时不玩贴吧,加上我觉得豆瓣挺良心的,我就爬了豆瓣首页上面的图片。其实最刚开始是想爬全站,后来一想我这简直是脑子犯抽,全站的图片爬下来得有多少,再说这个只是练一下手,所以就只爬取了首页上的图片。废话不多说 开始代码。
首先是主文件的代码:
import re
from html_downloder import HtmlDownloader
from html_downloder import Image
"'起始URL'"
url = "https://www.douban.com"
"'保存目录'"
image_path = "F:\source\Python\爬虫\ImageGet\Image%s.jpg"
"'定义实体类'"
downloader = HtmlDownloader()
html = downloader.download(url)
"'SaveFile(html, html_path)'"
html = html.decode('utf-8')
"'正则表达式'"
reg1 = r'="(https://img[\S]*?[jpg|png])"'
"'提取图片的URL'"
dbdata = re.findall(reg1, html)
imgsave = Image()
"'下载保存图片'"
imgsave.ImageGet(dbdata, image_path)
我们打开豆瓣首页然后看一下里面图片的url会发现
都是以“=”等号开头,后面接双引号,中间都是https://img,末尾以双引号结束。
因此我们的正则表达式可以写成 reg1 = r'="(https://img[\S]*?[jpg|png])"'
在这个表达式中"[]"中括号里面的东西会作为一个整体,其中[\S]表示大小写字母和数字,[jpg|png]表示以png结尾或者jpg结尾(在这次爬虫中并没有包括gif,因为打开gif的url发现是空白)。
然后是html_downloder.py的代码:
# file: html_downloader.py
import urllib.request
import urllib.error
import time
class HtmlDownloader(object):
def download(self, url):
if url is None:
return None
try:
header = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64)'}
"'发出请求'"
request = urllib.request.Request(url=url, headers=header)
"'获取结果'"
response = urllib.request.urlopen(url)
except urllib.error.URLError as e:
if hasattr(e, "code"):
print(e.code)
if hasattr(e, "reason"):
print(e.reason)
if response.getcode() != 200:
return None
html = response.read()
response.close()
return html
class Image (object):
def ImageGet(self, imageurl, image_path):
x = 0
for li in imageurl:
urllib.request.urlretrieve(li, image_path % x)
x = x + 1
"'休眠5s以免给服务器造成严重负担'"
time.sleep(5)
这个文件的代码主要是负责下载html网页和下载具体的图片。
接下来就可以在保存路径对应的文件夹中中看到下载的图片了
至此,爬虫告一段落,离大佬的路还远得很,继续加油!!
来源:http://www.cnblogs.com/establish/p/8127784.html
标签:Python,豆瓣爬虫
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python DataFrame中loc与iloc取数据的基本方法实例
2022-06-23 15:58:25
![](https://img.aspxhome.com/file/2023/8/67868_0s.png)
如何在Windows中安装多个python解释器
2023-08-28 02:55:43
![](https://img.aspxhome.com/file/2023/1/101011_0s.jpg)
也谈access数据库的防下载保护
2007-08-28 13:01:00
python 判断linux进程,并杀死进程的实现方法
2022-06-24 22:31:54
python写的一个文本编辑器
2021-10-12 08:38:04
php测试程序运行速度和页面执行速度的代码
2023-06-14 07:49:18
Python中使用Boolean操作符做真值测试实例
2021-05-31 01:18:19
Python实现根据日期获取当天凌晨时间戳的方法示例
2023-08-04 14:16:46
Python OpenGL绘制一场烟花盛会
2021-02-08 06:12:44
![](https://img.aspxhome.com/file/2023/7/97027_0s.png)
Android分包MultiDex策略详解
2021-11-14 13:28:42
Python编程实现控制cmd命令行显示颜色的方法示例
2023-07-24 05:39:45
![](https://img.aspxhome.com/file/2023/0/60940_0s.jpg)
Python SQLite3数据库操作类分享
2023-08-24 01:04:02
python简单实现基数排序算法
2023-11-10 06:27:27
wxPython色环电阻计算器
2023-12-21 20:08:54
详解python函数的闭包问题(内部函数与外部函数详述)
2023-01-22 20:53:01
python实现八大排序算法(2)
2023-09-05 06:28:23
![](https://img.aspxhome.com/file/2023/9/90779_0s.png)
python如何求解两数的最大公约数
2021-01-16 07:38:18
Django model反向关联名称的方法
2021-03-06 17:21:28
Python3.6 Schedule模块定时任务(实例讲解)
2022-08-14 15:55:00
![](https://img.aspxhome.com/file/2023/3/70073_0s.png)
利用Python实现读取Word表格计算汇总并写入Excel
2021-06-24 06:15:47
![](https://img.aspxhome.com/file/2023/0/93460_0s.jpg)