《从零开始学Python网络爬虫》CH6
2017-12-28 16:51
190 查看
Case1 爬取PEXELS图片
案例描述爬取知名图片网站PEXELS。可惜,目前已加入反爬机制,《从零开始学Python网络爬虫》CH6中给出的代码无法运行。
# -*- coding: utf-8 -*- import requests import time from bs4 import BeautifulSoup import os folder = 'Beauty' if not os.path.exists(folder): os.makedirs(folder) n = 0 header = {'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'} url = 'https://www.pexels.com/search/beauty/' response = requests.get(url, headers = header) html = response.text soup = BeautifulSoup(html, 'html.parser') srcs = soup.select('article > a > img') for src in srcs: img = src.get('src') print(img) print(n) res = requests.get(img, headers = header) f = open(folder + '/' + str(n) + '.jpg', 'wb') f.write(res.content) f.close() time.sleep(0.1) n += 1 print('done')
代码分析
Case2 爬取糗事百科网的用户地址信息
案例描述这里写代码片
代码分析
相关文章推荐
- 《从零开始学Python网络爬虫》实战案例目录
- 从零开始学python网络爬虫
- 《从零开始学Python网络爬虫》CH4
- 《从零开始学Python网络爬虫》CH12
- 《从零开始学Python网络爬虫》CH3
- 《从零开始学Python网络爬虫》CH5
- 《从零开始学Python网络爬虫》CH8
- 《从零开始学Python网络爬虫》CH7
- 《从零开始学Python网络爬虫》CH9
- 《从零开始学Python网络爬虫》CH10
- [Python]网络爬虫(一):抓取网页的含义和URL基本构成
- win7环境Python网络爬虫安装第三方库lxml出现问题
- 【Python网络爬虫学习01】爬取网络验证码
- 从零开始学Python学习笔记---之--pandas序列部分
- python3 网络爬虫(二)利用get请求获取网页的动态加载数据
- python自学笔记(9)--《用python写网络爬虫》之网站背景调研
- 从零开始python案例008分解质因数
- 从零开始学Python第二天
- 从零开始学Python06作业思路:学生选课系统
- 用Python从零开始创建区块链