今日头条街拍图片抓取
爬取目标:
今日头条右上角输入:街拍
之后得到的是这样一个磨人的小妖精
分析网页之后发现是Ajax技术(我用的是Chrom的开发者工具,点XHR之后发现了常规的Ajax请求)
然后在细致的分析Preview里面的代码。会发现image_list里面包含了图片的链接,title里面包含了名称。这就是我们需要爬取的部分了如图
然后回到Headers,我们根据Request URL来构造GET请求。如下图
其中唯一的变化参数是offset,将其作为参数。来构造单个的Ajax请求。。代码如下:
def get_page(offset):
params = {
'offset': offset,
'format': 'json',
'keyword': '街拍',
'autoload': 'true',
'count': '20',
'cur_tab': '1',
'from': 'search_tab',
}
url = 'https://www.toutiao.com/search_content/?' + urlencode(params)
try:
response = requests.get(url)
if response.status_code == 200:
return response.json()
except requests.ConnectError:
return None
然后提取每一张图片的链接和名字,代码如下:
def get_images(json):
data = json.get('data')
if data:
for item in data:
title = item.get('title')
image_list = item.get('image_list')
if image_list:
for image in image_list:
yield {
'image': image.get('url'),
'title': title
}
在接下来就是保存了。崔大佬用的是名字来当做新建文件夹的名称,每一张图片的名字是其内容的MD5值,这样可以去除重复,代码如下:
def save_image(item):
if not os.path.exists(item.get('title')):
os.mkdir(item.get('title'))
try:
local_image_url = item.get('image')
new_image_url = local_image_url.replace('list','large')
response = requests.get('http:'+ new_image_url)
if response.status_code == 200:
file_path = '{0}/{1}.{2}'.format(item.get('title'), md5(response.content).hexdigest(), 'jpg')
if not os.path.exists(file_path):
with open(file_path, 'wb') as f:#
f.write(response.content)
else:
print('Already Downloaded', file_path)
except requests.ConnectionError:
print('Failed to Save Image')
最后运行结果如下:
我感觉崔大佬代码很简练,学习他的还是需要一些python 的基础知识。重点应该学习一下他的编程思想。
不过觉得这个程序应该是用多进程下的图片,书上的原话是多线程。。。(采用与廖雪峰的对比)
也不知道是不是。不过大佬的书值得学习。也正在学习。目前市面上爬虫最好的一本了。附上最终完整代码:
import os
import requests
from urllib.parse import urlencode
from hashlib import md5
from multiprocessing.pool import Pool
GROUP_START = 0
GROUP_END = 2
def get_page(offset):
params = {
'offset': offset,
'format': 'json',
'keyword': '街拍',
'autoload': 'true',
'count': '20',
'cur_tab': '1',
'from': 'search_tab',
}
url = 'https://www.toutiao.com/search_content/?' + urlencode(params)
try:
response = requests.get(url)
if response.status_code == 200:
return response.json()
except requests.ConnectError:
return None
def get_images(json):
data = json.get('data')
if data:
for item in data:
title = item.get('title')
image_list = item.get('image_list')
if image_list:
for image in image_list:
yield {
'image': image.get('url'),
'title': title
}
def save_image(item):
if not os.path.exists(item.get('title')):
os.mkdir(item.get('title'))
try:
local_image_url = item.get('image')
new_image_url = local_image_url.replace('list','large')
response = requests.get('http:'+ new_image_url)
if response.status_code == 200:
file_path = '{0}/{1}.{2}'.format(item.get('title'), md5(response.content).hexdigest(), 'jpg')
if not os.path.exists(file_path):
with open(file_path, 'wb') as f:#
f.write(response.content)
else:
print('Already Downloaded', file_path)
except requests.ConnectionError:
print('Failed to Save Image')
def main(offset):
json = get_page(offset)
for item in get_images(json):
print(item)
save_image(item)#
if __name__ == '__main__':
pool = Pool()
groups = ([x*20 for x in range(GROUP_START, GROUP_END+1)])
pool.map(main, groups)
pool.close()
pool.join()
今日头条街拍图片抓取相关推荐
- 今日头条街拍图片爬取
其中遇到的问题和一些新知识: 1. 注意页面请求参数:(会改变) 即Query String Parameters 例: 今日头条里街拍综合的数据为 'offset': 0, 'format': 'j ...
- 爬取今日头条街拍图片
** *爬取今日头条街拍图片 * ** # coding=utf-8 import os import re import time from multiprocessing.pool import ...
- Scrapy 爬取今日头条街拍图片
scrapy 爬取今日头条图片保存至本地 之前用 requests 爬取过今日头条街拍的图片,当时只是爬取每篇文章的缩略图,今天尝试用 scrapy 来大规模爬取街拍详细图片. 分析页面 今日头条的内 ...
- Python爬虫:爬取今日头条“街拍”图片(修改版)
前言 在参考<Python3网络爬虫开发实战>学习爬虫时,练习项目中使用 requests ajax 爬取今日头条的"街拍"图片,发现书上的源代码有些已经不适合现在了, ...
- 我的爬虫 之 爬今日头条街拍图片
近日学习了python 爬虫方面的内容 ,决定实战--爬今日头条的街拍图片 首先先分析今日头条的请求方式,进入https://www.toutiao.com F12 搜索街拍 查看当前请求 http ...
- python爬今日头条组图_python 爬虫抓取今日头条街拍图片
1. 打开google浏览器,输入www.toutiao.com, 搜索街拍.html 2.打开开发者选项,network监看加载的xhr, 数据是ajax异步加载的,能够看到preview里面的da ...
- python爬取今日头条_爬取今日头条街拍图片
参考于崔庆才的Python爬虫教程,但是崔的视频时间过长,今日头条网站反爬虫也有了变化,因此写下此文章来记录自己的爬取过程遇到的问题,也给大家一些借鉴.欢迎大家讨论. 一.获取索引页. 我们会发现do ...
- 使用ajax爬取今日头条街拍图片
文章目录 分析请求 获取一组信息 解析json 获取图片列表 本地存储 整合功能 分析请求 地址:https://www.toutiao.com/search/?keyword=%E8%A1%97%E ...
- 分析Ajax爬取今日头条街拍图片
# -*- coding:UTF-8 -*- import requests import time import os from hashlib import md5def get_page(off ...
最新文章
- 基于sparksql调用shell脚本运行SQL
- 具备这四点品质的建站系统就爱了吧!
- [翻译] Entity Framework Core in Action 关于这本书
- BZOJ 4810 [Ynoi2017]由乃的玉米田(莫队+bitset)
- 请郎平回归执教中国女排
- hlsl之ambient
- hdu2074java
- java模拟器1.4.2_j2me模拟器下载
- Windows的CRT中的setlocale()
- Springboot启动报错:Failed to process import candidates for configuration class...
- 安装JDK报错“当前页面的脚本发生错误”解决方案
- 数据可视化-Tableau-网店运营可视化分析
- java消息中间件_java消息中间件
- 小伙子不讲武德,竟用Python爬取了B站上1.4w条马老师视频数据来分析
- 方差分析/卡方/fisher精确检验
- hdoj-2550 百步穿杨
- mysql协议分析 row_MySQL协议分析(结合PyMySQL)
- 不愧是阿里巴巴公布Java10W字面经,在Github标星32K
- 研发团队管理经验总结(一)
- STC8H库函数使用笔记