博客
关于我
Python爬取堆糖网优美古风头像(附源码)
阅读量:634 次
发布时间:2019-03-14

本文共 1207 字,大约阅读时间需要 4 分钟。

前言

文中文字及图片均来源网络供学习交流使用,版权归原作者,若有问题请联系处理。

若想完成爬虫程序,需先安装以下工具包:

  • requests 网络库
  • bs4 页面选择器

安装步骤:

  • pip install requests
  • pip install bs4

os 是 Python 自带工具库,用于文件操作。

SSL 证书支持:

https 网站基于 SSL 加密传输,需注意证书验证。

一般爬虫流程

1. 模拟浏览器访问目标网站 2. 使用 requests 发起 HTTP 请求获取网页数据 3. 通过 bs4 筛选所需数据 4. 根据规则下载图片

本次目标

爬取堆糖网图片: 目标网站:https://www.duitang.com/

爬虫代码

导入工具: ```pythonimport requestsimport osfrom bs4 import BeautifulSoup```

设置请求头:

headers = {    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36'}

证书验证:

import sslssl._create_default_https_context = ssl._create_unverified_context

图片爬取函数:

def get_images(url):    images_html = requests.get(url, headers=headers).text    soup = BeautifulSoup(images_html, 'lxml')    images_list = soup.find_all('div', class_='mbpho')        for image in images_list:        image_data = image.find('a', class_='a')        image_url = image_data.find('img')['src']        image_id = image_data.find('img')['data-rootid']                try:            urllib.request.urlretrieve(image_url, f'./古风头像/{image_id}{os.path.splitext(image_url)[-1]}')            print('下载成功...')        except:            pass

效果图

(以下均为图片链接,已去除外部资源引用)

转载地址:http://cqioz.baihongyu.com/

你可能感兴趣的文章
NoSQL&MongoDB
查看>>
NoSQL介绍
查看>>
Notepad ++ 安装与配置教程(非常详细)从零基础入门到精通,看完这一篇就够了
查看>>
Notepad++在线和离线安装JSON格式化插件
查看>>
notepad++最详情汇总
查看>>
notepad如何自动对齐_notepad++怎么自动排版
查看>>
Notification 使用详解(很全
查看>>
NotImplementedError: Cannot copy out of meta tensor; no data! Please use torch.nn.Module.to_empty()
查看>>
Now trying to drop the old temporary tablespace, the session hangs.
查看>>
nowcoder—Beauty of Trees
查看>>
np.arange()和np.linspace()绘制logistic回归图像时得到不同的结果?
查看>>
npm error Missing script: “server“npm errornpm error Did you mean this?npm error npm run serve
查看>>
npm error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。要解决此问题,1) 安装
查看>>
npm install digital envelope routines::unsupported解决方法
查看>>
npm install 卡着不动的解决方法
查看>>
npm install 报错 EEXIST File exists 的解决方法
查看>>
npm install 报错 ERR_SOCKET_TIMEOUT 的解决方法
查看>>
npm install 报错 fatal: unable to connect to github.com 的解决方法
查看>>
npm install 报错 no such file or directory 的解决方法
查看>>
npm install报错,证书验证失败unable to get local issuer certificate
查看>>