分析ajax请求抓取今日头条关键字美图
# 目标:抓取今日头条关键字美图
# 思路:
# 一、分析目标站点
# 二、构造ajax请求,用requests请求到索引页的内容,正则+BeautifulSoup得到索引url
# 三、对索引url请求,得到图片url与标题,下载并保存到数据库,本次使用MongDB
# 四、开启循环与多进程,对多页内容遍历与抓取 #问题一、为什么要构造请求
#为什么要构造请求,举个例子,第一屏的内容我们看到的实际url是:
# http://www.toutiao.com/search_content/?offset=20&format=json&keyword=%E8%A1%97%E6%8B%8D&autoload=true&count=20&cur_tab=1
# 后面有一大串参数,这些参数就是请求的一些‘设定’,表示关键词,加载的页数,等等,是一个字典的形式,
# 如果人为去传这些数据显然十分繁琐,我们需要将这字典编码成一定格式加载请求函数里面。
import os
from json import JSONDecodeError
from multiprocessing.pool import Pool import requests
from urllib.parse import urlencode
import json
import pymongo from bs4 import BeautifulSoup from requests.exceptions import RequestException
import re
from config import * client = pymongo.MongoClient(MONGO_URL)
db = client[MONGO_DB] def get_index_page(offset,keyword):
data = {
'offset': offset,
'format': 'json',
'keyword': keyword,
'autoload': 'true',
'count': '',
'cur_tab': 1
}
data = urlencode(data)
url ='http://www.toutiao.com/search_content/?' + data
#print(url)
try:
response = requests.get(url)
if response.status_code == 200:
return response.text
else:
return None
except RequestException:
print('请求不到索引页面!')
return None def parse_index_page(html): #json_obj = json.dumps(html)#将Python对象序列化为json
#python_obj = json.loads(json_obj)#将json加载成Python对象
data = json.loads(html)
#在进行json操作之前有必要了解一下json是怎么操作的
if data and 'data' in data.keys():
for item in data.get('data'):
yield item.get('article_url') def get_detail_page(url):
try:
response = requests.get(url)
if response.status_code == 200:
return response.text
else:
return None
except RequestException:
return None def save_to_mongo(result):
if db[MONG_TABLE].insert(result):
print('存储到MongoDB成功',result)
return True
else:
return False def parse_detail_page(html,url):
soup = BeautifulSoup(html,'lxml')
title = soup.title.string
pattern = re.compile(r'var gallery = (.*?);',re.S)
result = re.findall(pattern,html)
if result:
images=[]
for i in result:
i = json.loads(i)
j = i.get("sub_images")
#print(j)
for k in j:
k = k.get('url')
images.append(k) return{
'title':title,
'url':url,
'images':images
} def download_image(result):
image_list = result.get('images')
image_title = result.get('title')
print('正在下载:%s'%image_title) if image_title not in os.listdir(path ='.'):
os.mkdir(image_title)
os.chdir(image_title)
for image in image_list:
try:
response = requests.get(image)
if response.status_code == 200:
filename = image.split('/')[-1] + '.jpg'
with open(filename,'wb') as f:
f.write(response.content)
print('正在下载:%s'%image) else:
return None
except RequestException:
return None
os.chdir(os.pardir)#返回上一级目录 def main(offset): html = get_index_page(offset,KEYWORDS)
for url in parse_index_page(html):
#print(url)
html = get_detail_page(url)
if html:
result = parse_detail_page(html,url)
if result:
#print(result)
#save_to_mongo(result)
download_image(result) if __name__ == '__main__': groups = [i*20 for i in range(GROUP_START,GROUP_END + 1)]
pool = Pool()
pool.map(main,groups)
#对比老司机所写
import json
import os
from urllib.parse import urlencode
import pymongo
import requests
from bs4 import BeautifulSoup
from requests.exceptions import ConnectionError
import re
from multiprocessing import Pool
from hashlib import md5
from json.decoder import JSONDecodeError
from config import * client = pymongo.MongoClient(MONGO_URL, connect=False)
db = client[MONGO_DB] def get_page_index(offset, keyword):
data = {
'autoload': 'true',
'count': 20,
'cur_tab': 3,
'format': 'json',
'keyword': keyword,
'offset': offset,
}
params = urlencode(data)
base = 'http://www.toutiao.com/search_content/'
url = base + '?' + params
try:
response = requests.get(url)
if response.status_code == 200:
return response.text
return None
except ConnectionError:
print('Error occurred')
return None def download_image(url):
print('Downloading', url)
try:
response = requests.get(url)
if response.status_code == 200:
save_image(response.content)
return None
except ConnectionError:
return None def save_image(content):
file_path = '{0}/{1}.{2}'.format(os.getcwd(), md5(content).hexdigest(), 'jpg')
print(file_path)
if not os.path.exists(file_path):
with open(file_path, 'wb') as f:
f.write(content)
f.close() def parse_page_index(text):
try:
data = json.loads(text)
if data and 'data' in data.keys():
for item in data.get('data'):
yield item.get('article_url')
except JSONDecodeError:
pass def get_page_detail(url):
try:
response = requests.get(url)
if response.status_code == 200:
return response.text
return None
except ConnectionError:
print('Error occurred')
return None def parse_page_detail(html, url):
soup = BeautifulSoup(html, 'lxml')
result = soup.select('title')
title = result[0].get_text() if result else ''
images_pattern = re.compile('var gallery = (.*?);', re.S)
result = re.search(images_pattern, html)
if result:
data = json.loads(result.group(1))
if data and 'sub_images' in data.keys():
sub_images = data.get('sub_images')
images = [item.get('url') for item in sub_images]
for image in images: download_image(image)
return {
'title': title,
'url': url,
'images': images
} def save_to_mongo(result):
if db[MONGO_TABLE].insert(result):
print('Successfully Saved to Mongo', result)
return True
return False def main(offset):
text = get_page_index(offset, KEYWORD)
urls = parse_page_index(text)
for url in urls:
html = get_page_detail(url)
result = parse_page_detail(html, url)
if result: save_to_mongo(result) pool = Pool()
groups = ([x * 20 for x in range(GROUP_START, GROUP_END + 1)])
pool.map(main, groups)
pool.close()
pool.join()
分析ajax请求抓取今日头条关键字美图的更多相关文章
- python学习(26)分析ajax请求抓取今日头条cosplay小姐姐图片
分析ajax请求格式,模拟发送http请求,从而获取网页代码,进而分析取出需要的数据和图片.这里分析ajax请求,获取cosplay美女图片. 登陆今日头条,点击搜索,输入cosplay 下面查看浏览 ...
- 爬虫(八):分析Ajax请求抓取今日头条街拍美图
(1):分析网页 分析ajax的请求网址,和需要的参数.通过不断向下拉动滚动条,发现请求的参数中offset一直在变化,所以每次请求通过offset来控制新的ajax请求. (2)上代码 a.通过aj ...
- Python爬虫学习==>第十一章:分析Ajax请求-抓取今日头条信息
学习目的: 解决AJAX请求的爬虫,网页解析库的学习,MongoDB的简单应用 正式步骤 Step1:流程分析 抓取单页内容:利用requests请求目标站点,得到单个页面的html代码,返回结果: ...
- 通过分析Ajax请求 抓取今日头条街拍图集
代码: import os import re import json import time from hashlib import md5 from multiprocessing import ...
- python3爬虫-分析Ajax,抓取今日头条街拍美图
# coding=utf-8 from urllib.parse import urlencode import requests from requests.exceptions import Re ...
- python爬虫---实现项目(二) 分析Ajax请求抓取数据
这次我们来继续深入爬虫数据,有些网页通过请求的html代码不能直接拿到数据,我们所需的数据是通过ajax渲染到页面上去的,这次我们来看看如何分析ajax 我们这次所使用的网络库还是上一节的Reques ...
- python爬虫之分析Ajax请求抓取抓取今日头条街拍美图(七)
python爬虫之分析Ajax请求抓取抓取今日头条街拍美图 一.分析网站 1.进入浏览器,搜索今日头条,在搜索栏搜索街拍,然后选择图集这一栏. 2.按F12打开开发者工具,刷新网页,这时网页回弹到综合 ...
- 分析Ajax来爬取今日头条街拍美图并保存到MongDB
前提:.需要安装MongDB 注:因今日投票网页发生变更,如下代码不保证能正常使用 #!/usr/bin/env python #-*- coding: utf-8 -*- import json i ...
- 15-分析Ajax请求并抓取今日头条街拍美图
流程框架: 抓取索引页内容:利用requests请求目标站点,得到索引网页HTML代码,返回结果. 抓取详情页内容:解析返回结果,得到详情页的链接,并进一步抓取详情页的信息. 下载图片与保存数据库:将 ...
随机推荐
- 简洁明了的插值音频重采样算法例子 (附完整C代码)
近一段时间在图像算法以及音频算法之间来回游走. 经常有一些需求,需要将音频进行采样转码处理. 现有的知名开源库,诸如: webrtc , sox等, 代码阅读起来实在闹心. 而音频重采样其实也就是插值 ...
- 【Python】excel读写操作 xlrd & xlwt
xlrd ■ xlrd xlrd模块用于读取excel文件内容 基本用法: workbook = xlrd.open_workbook('文件路径') workbook.sheet_names() # ...
- KVM之一:安装准备(基于CentOS6.7)
KVM 虚拟机简介: Kernel-based Virtual Machine的简称,是一个开源的系统虚拟化模块,自Linux 2.6.20之后集成在Linux的各个主要发行版本中.它使用Linux自 ...
- 开篇/javascript基础知识点
html css js 分别是一个网站的:内容 样式 行为: js 的三种样式:行内 内嵌 外链. 函数的特性:1.可以重复执行的代码块.2.不调用不执行.3.要访问里面,必须先执行. 内置对象:j ...
- 使用.NET开发AutoCAD——设计师不做画图匠(一)
(一)前言--如何避免加班那些事 我是谁?我是一名工程设计师,有点"不务正业",在工作之余长期从事软件开发工作,开发了公路铁路行业广泛应用的设计软件.说正题之前,聊聊加班那些事.话 ...
- 新事物学习---Chrome上使用PWA
PWA是什么 PWA(Progressive Web Apps)是 Google 最近在提的一种 Web App 形态 (或者如 Wikipedia 所称的"软件开发方法").PW ...
- spring框架学习笔记4:SpringAOP实现原理
AOP AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善.OOP引入 ...
- 《统计学习方法》P89页IIS的中间步骤Zw+δ(X)/Zw(X)的推导
共有两个方法:
- winform 适配high dpi
在 mainifest文件中添加:(新建mainifest文件的时候以下内容是有的,只要取消注释就可以了) <compatibility xmlns="urn:schemas-micr ...
- 《Language Implementation Patterns》之 强类型规则
语句的语义取决于其语法结构和相关符号:前者说明了了要"做什么",后者说明了操作"什么对象".所以即使语法结构正确的,如果被操作的对象不合法,语句也是不合法的.语 ...