php网页抓取图片( 运行爬虫实战系列教你用爬虫爬取*敏*感*词*姐图片(解析输入参数))
优采云 发布时间: 2022-04-20 09:38php网页抓取图片(
运行爬虫实战系列教你用爬虫爬取*敏*感*词*姐图片(解析输入参数))
在上一篇文章中,有网友评论说不会在仓库里运行爬虫代码,所以本帖特地做了一个专题来演示一下。同样的,在广大兄弟的建议下,同时为了增加大家学习爬虫的兴趣,今天给大家讲解一下如何爬取*敏*感*词*姐们的图片。
我们以煎蛋为例(网址:),登录网站,哇,风景好美,你要坚持住,我们是来学爬的。
我们用google浏览器打开网站,然后按F12分析请求过程。可以看出还是比较简单的,就是一个简单的get动作,只要配置好要请求的参数即可。
好了,现在简单说一下爬虫的要点:
1.通过for循环多次请求获取多页内容
2.从返回的结果中提取图片资源并保存到本地
2.为了保证保存的内容不重复,需要记录保存图片的路径资源
使用的库:
1.requests(请求资源使用情况)
2.beautifulsoup(解析网页内容)
3.argparse(解析输入参数)
核心代码如下:
# 保存图片
def save_jpg(res_url):
global index
html = BeautifulSoup(requests.get(res_url, headers=headers).text, features="html.parser")
for link in html.find_all('a', {'class': 'view_img_link'}):
if link.get('href') not in image_cache: # 不在数据库里的才进行存储
with open(
'{}/{}.{}'.format(_dir, index, link.get('href')[len(link.get('href')) - 3: len(link.get('href'))]),
'wb') as jpg:
jpg.write(requests.get("http:" + link.get('href')).content)
image_cache.add(link.get('href'))
print("正在抓取第%s条数据" % index)
index += 1
我已经在workbench库中维护了完整的程序,路径是(workbenchmodulescrawlerjandan_spider_bs4jandan_spider.py)
或者去论坛直接复制保存到本地运行
爬虫实战系列教你用爬虫爬取*敏*感*词*姐的照片
运行程序:
我们将命令行切换到jandan_spider.py文件所在的路径,运行如下命令:
[workbench] D:workbenchmodulescrawlerjandan_spider_bs4>python jandan_spider.py
WARNING: You are using pip version 20.1.1; however, version 20.2 is available.
You should consider upgrading via the 'd:softanaconda3envsworkbenchpython.exe -m pip install --upgrade pip' command.
pip install argparse
Looking in indexes: http://pypi.douban.com/simple
Requirement already satisfied: argparse in d:softanaconda3envsworkbenchlibsite-packages (1.4.0)
WARNING: You are using pip version 20.1.1; however, version 20.2 is available.
You should consider upgrading via the 'd:softanaconda3envsworkbenchpython.exe -m pip install --upgrade pip' command.
正在抓取第0条数据
正在抓取第1条数据
正在抓取第2条数据
正在抓取第3条数据
......
正在抓取第119条数据
正在抓取第120条数据
刷新本地目录,可以看到本地已经保存了120张图片,也可以修改页面参数获取更多图片。
其实这样的图还有很多网站,就举两个吧?没问题:
%3D1
不同网站网页结构略有不同,有的是静态的,有的需要XHR动态加载,大家在实现自己的爬虫程序时需要灵活。
参考资料: