python3爬虫-分析Ajax,抓取今日头条街拍美图
# coding=utf-8
from urllib.parse import urlencode
import requests
from requests.exceptions import RequestException,Timeout
import json
from bs4 import BeautifulSoup
from pymongo import MongoClient
from multiprocessing import Pool
import os
import string
from hashlib import md5 def get_response(url):
try:
headers = {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.62 Safari/537.36"
}
# proxies = {'http':'118.11.2.3:8080'}
response = requests.get(url, headers=headers, timeout=5)
print(url + 'request success')
return response
except Timeout:
print(url + 'request timeout') def get_page_index(offset, keyword): data = {
"offset": offset,
"format": "json",
"keyword": keyword,
"autoload": "true",
"count": "20",
"cur_tab": "1",
"from":"search_tab"
} url = "https://www.toutiao.com/search_content/?" + urlencode(data)
print(url)
try:
response = get_response(url)
print(response.status_code)
if response.status_code == 200:
return response.text
return None
except RequestException:
print('request error')
return None def conn_mongodb():
client = MongoClient('localhost', 27017)
db = client['jiepai']
jiepai = db['jiepai']
return jiepai def save_image_url(data):
jiepai = conn_mongodb()
jiepai.update({'title':data.get('title')}, {'$set':data}, upsert=True) def get_image_url():
jiepai = conn_mongodb()
data = jiepai.find({}, {'title': 1, 'images_list': 1, '_id': 0})
return data def download_image(data): base_dir = os.path.abspath(os.path.dirname(__file__))
if not os.path.exists(base_dir + '\jiepai'):
os.mkdir(base_dir + '\jiepai')
for item in data:
print(item.get('title'))
title = item.get('title')
images_list = item.get('images_list')
print('images_lsit',images_list)
# every file name
file_name = title.strip(string.punctuation)
file_name = str(file_name).replace('?','')
if not os.path.exists(base_dir + '\jiepai/' + file_name):
os.mkdir(base_dir + '\jiepai\\' + file_name)
# save images path
file_path = base_dir + '\jiepai\\' + file_name
for image_url in images_list:
print(image_url)
response = get_response(image_url)
html = response.content
image_name = md5(html).hexdigest() + '.jpg' with open(file_path + '\\' + image_name, 'wb') as f:
f.write(html)
print('download success') def parse_page_index(html):
data = json.loads(html)
if data and 'data' in data.keys():
for item in data.get('data'):
a_gourp_image_detail = {}
images_list = []
title = item.get('title')
# print(title)
if title is not None:
a_gourp_image_detail['title'] = title
images = item.get('image_detail')
# print(images)
if images:
for image in images:
# print(image.get('url'))
images_list.append(image.get('url'))
# if images_list:
a_gourp_image_detail['images_list'] = list(set(images_list))
print(a_gourp_image_detail)
save_image_url(a_gourp_image_detail) def main(offset): html = get_page_index(offset, '街拍')
# print(html)
parse_page_index(html) if __name__ == "__main__":
# 多进程爬取图片链接,并保存到 Mongodb
# groups = [x*20 for x in range(0,5)]
# pool = Pool()
# pool.map(main, groups) # 从 mongodb 中获取链接,多进程下载图片,并保存
data = get_image_url()
datas = [item for item in data] pool = Pool()
pool.map(download_image, data)
# download_image()
python3爬虫-分析Ajax,抓取今日头条街拍美图的更多相关文章
- 分析Ajax抓取今日头条街拍美图
spider.py # -*- coding:utf-8 -*- from urllib import urlencode import requests from requests.exceptio ...
- 【Python3网络爬虫开发实战】 分析Ajax爬取今日头条街拍美图
前言本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理.作者:haoxuan10 本节中,我们以今日头条为例来尝试通过分析Ajax请求 ...
- 分析Ajax爬取今日头条街拍美图-崔庆才思路
站点分析 源码及遇到的问题 代码结构 方法定义 需要的常量 关于在代码中遇到的问题 01. 数据库连接 02.今日头条的反爬虫机制 03. json解码遇到的问题 04. 关于response.tex ...
- 关于爬虫的日常复习(9)—— 实战:分析Ajax抓取今日头条接拍美图
- python爬虫之分析Ajax请求抓取抓取今日头条街拍美图(七)
python爬虫之分析Ajax请求抓取抓取今日头条街拍美图 一.分析网站 1.进入浏览器,搜索今日头条,在搜索栏搜索街拍,然后选择图集这一栏. 2.按F12打开开发者工具,刷新网页,这时网页回弹到综合 ...
- 15-分析Ajax请求并抓取今日头条街拍美图
流程框架: 抓取索引页内容:利用requests请求目标站点,得到索引网页HTML代码,返回结果. 抓取详情页内容:解析返回结果,得到详情页的链接,并进一步抓取详情页的信息. 下载图片与保存数据库:将 ...
- Python Spider 抓取今日头条街拍美图
""" 抓取今日头条街拍美图 """ import os import time import requests from hashlib ...
- 分析Ajax请求并抓取今日头条街拍美图
项目说明 本项目以今日头条为例,通过分析Ajax请求来抓取网页数据. 有些网页请求得到的HTML代码里面并没有我们在浏览器中看到的内容.这是因为这些信息是通过Ajax加载并且通过JavaScript渲 ...
- 【Python3网络爬虫开发实战】6.4-分析Ajax爬取今日头条街拍美图【华为云技术分享】
[摘要] 本节中,我们以今日头条为例来尝试通过分析Ajax请求来抓取网页数据的方法.这次要抓取的目标是今日头条的街拍美图,抓取完成之后,将每组图片分文件夹下载到本地并保存下来. 1. 准备工作 在本节 ...
- 转:【Python3网络爬虫开发实战】6.4-分析Ajax爬取今日头条街拍美图
[摘要] 本节中,我们以今日头条为例来尝试通过分析Ajax请求来抓取网页数据的方法.这次要抓取的目标是今日头条的街拍美图,抓取完成之后,将每组图片分文件夹下载到本地并保存下来. 1. 准备工作 在本节 ...
随机推荐
- ie6、ie7下JSON.parse JSON未定义的解决方法
解决方法一: var jsons = req.responseText; var s; if (typeof(JSON) == 'undefined'){ s = eval("(" ...
- LVM详解笔记pv-vg-lv创建和扩展
LVM Logical Volume Manager(逻辑卷管理) 是Linux环境下对底层磁盘的一种管理机制(方式),处在物理磁盘和文件系统之间. 名词: PV (Physical Volume)物 ...
- FPGA开发要懂得使用硬件分析仪调试——ILA
0. ILA概述在FPGA开发中,当我们写完代码,进行仿真,确定设计没有问题后,下载到硬件上一般都能按照我们的设计意愿执行相应功能.但这也并非绝对的,有时候你会遇到一些突然情况,比如时序问题或者仿真时 ...
- C语言基础(19)-结构体,联合体,枚举和typedef
一.结构体 1.1 结构体struct定义及初始化 #include <stdio.h> // 这个头文件在系统目录下 #include <stdlib.h> // 使用了sy ...
- 李洪强iOS开发之OC[003] - 用钥匙串存储信息模拟登陆
- Vue 组件5 高级异步组件
自2.3.0起,异步组件的工厂函数也可以返回一个如下的对象. const AsyncComp = () => ({ // 需要加载的组件. 应当是一个 Promise component: im ...
- volatile关键字比较好的解释
http://www.tuicool.com/articles/IRvUJbN http://tengj.top/2016/05/06/threadvolatile4/?utm_source=tuic ...
- hibernate查询之后用el表达式取值时遇到的问题
String juniorApprovalUserHql = "select c.id,c.username from UserInfo c left join c.userRole whe ...
- CSS学习(二)- 有关 hasLayout 和 BFC
1. hasLayout 概念说明 ‘Layout’ 可以被某些 CSS property(特性)不可逆的触发,而某些 HTML 元素本身就具有 layout . ‘Layout’ 在 IE 中可以通 ...
- Nginx区分PC或手机访问不同网站
近几年来,随着手机和pad的普及,越来越多的用户选择使用移动客户端访问网站,而为了获取更好的用户体验,就需要针对不同的设备显示出最合适的匹配,这样就是近年来流行的“响应式web设计”. 响应式web设 ...