python爬虫实例之获取动漫截图
作者:四月一日 君寻 时间:2023-08-17 22:13:57
引言
之前有些无聊(呆在家里实在玩的腻了),然后就去B站看了一些python爬虫视频,没有进行基础的理论学习,也就是直接开始实战,感觉跟背公式一样的进行爬虫,也算行吧,至少还能爬一些东西,hhh。我今天来分享一个我的爬虫代码。
正文
话不多说,直接上完整代码
ps:这个代码有些问题 每次我爬到fate的图片它就给我报错,我只好用个try来跳过了,如果有哪位大佬能帮我找出错误并给与纠正,我将不胜感激
import requests as r
import re
import os
import time
file_name = "动漫截图"
if not os.path.exists(file_name):
os.mkdir(file_name)
for p in range(1,34):
print("--------------------正在爬取第{}页内容------------------".format(p))
url = 'https://www.acgimage.com/shot/recommend?page={}'.format(p)
headers = {"user-agent"
: "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.162 Safari/537.36"}
resp = r.get(url, headers=headers)
html = resp.text
images = re.findall('data-original="(.*?)" ', html)
names =re.findall('title="(.*?)"', html)
#print(images)
#print(names)
dic = dict(zip(images, names))
for image in images:
time.sleep(1)
print(image, dic[image])
name = dic[image]
#name = image.split('/')[-1]
i = r.get(image, headers=headers).content
try:
with open(file_name + '/' + name + '.jpg' , 'wb') as f:
f.write(i)
except FileNotFoundError:
continue
先导入要使用的库
import requests as r
import re
import os
import time
然后去分析要去爬的网址: https://www.acgimage.com/shot/recommend
下图是网址的内容:
好了 url已经确定
下面去寻找headers
找到user-agent 将其内容复制到headers中
第一步就完成了
下面是代码展示
url = 'https://www.acgimage.com/shot/recommend?page={}'.format(p)
headers = {"user-agent"
: "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.162 Safari/537.36"
}
然后检索要爬的图片内容
从上图就可以找到图片的位置:data-origina=后面的内容
以及图片的名字:title=后面的内容
然后用正则表达式re来检索就行了
images = re.findall('data-original="(.*?)" ', html)
names =re.findall('title="(.*?)"', html)
最后将其保存就好了
i = r.get(image, headers=headers).content
with open(file_name + '/' + name + '.jpg' , 'wb') as f:
f.write(i)
还有就是一些细节了
比如换页
第一页网址:
https://www.acgimage.com/shot/recommend
第二页网址:https://www.acgimage.com/shot/recommend?page=2
然后将page后面的数字改动就可以跳到相应的页面
换页的问题也就解决了
or p in range(1,34):
url = 'https://www.acgimage.com/shot/recommend?page={}'.format(p)
以及将爬到的图片放到自己建立的文件zh
使用了os库
file_name = "动漫截图"
if not os.path.exists(file_name):
os.mkdir(file_name)
以及为了不影响爬取的网站 使用了sleep函数
虽然爬取的速度慢了一些
但是这是应遵守的道德
time.sleep(1)
以上 这就是我的爬虫过程
还是希望大佬能解决我的错误之处
万分感谢
来源:https://blog.csdn.net/qq_45843334/article/details/106174204
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
在Python中操作列表之list.extend()方法的使用
Django给admin添加Action的步骤详解
![](https://img.aspxhome.com/file/2023/9/131659_0s.png)
如何提升JavaScript的运行速度(递归篇)
详解如何通过Python制作一个密码生成器
![](https://img.aspxhome.com/file/2023/6/63986_0s.png)
关于vue中的时间格式转化问题
![](https://img.aspxhome.com/file/2023/4/125274_0s.png)
Python实现轻松切割MP3文件
![](https://img.aspxhome.com/file/2023/5/62605_0s.png)
PHP 截取字符串 分别适合GB2312和UTF8编码情况
基于python的itchat库实现微信聊天机器人(推荐)
![](https://img.aspxhome.com/file/2023/2/64712_0s.png)
python中的元组与列表及元组的更改
mysql中GROUP_CONCAT的使用方法实例分析
![](https://img.aspxhome.com/file/2023/3/124883_0s.png)
Python读取一个目录下所有目录和文件的方法
Oracle SQL性能优化系列学习三
Python 描述符(Descriptor)入门
window.top[_CACHE]实现多个jsp页面共享一个js对象
C#实现按数据库邮件列表发送邮件的方法
Python中fnmatch模块的使用详情
使用用SQL语句从电脑导入图片到数据库的方法
全面解析python当前路径和导包路径问题
Python实现简单的列表冒泡排序和反转列表操作示例
IE bug: 1像素的dotted/dashed边框
![](https://img.aspxhome.com/file/UploadPic/200910/28/dotted-1px-59s.jpg)