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. 准备工作 在本节 ...
随机推荐
- cygwin开发环境搭建与apt-cyg的应用
1.Cygwin安装 http://www.cygwin.com/下载安装工具 详细安装过程參照http://jingyan.baidu.com/article/6b97984d83dfe51ca2b ...
- JDBC的常用API
一.Connection接口: 1.createStatement():创建数据库连接 2.prepareStatement(Stringsql):创建预处理语句 3.prepareCall(Stri ...
- fpga技能树
- 转:NHibernate 存储过程
http://stackoverflow.com/questions/928847/how-to-get-the-return-value-from-a-sql-server-stored-proce ...
- [HNOI2008]玩具装箱toy(dp+斜率优化)
斜率优化问题一般都是决策单调问题.对于这题能够证明单调决策. 令sum[i]=sigma(c [k] ) 1<=k<=i , f[i]=sum[i]+i , c=L+1; 首先我们能 ...
- MapReduce-MulitipleOutputs实现自己定义输出到多个文件夹
输入源数据例子: Source1-0001 Source2-0002 Source1-0003 Source2-0004 Source1-0005 Source2-0006 Source3-0007 ...
- 辛星浅析Linux中的postfix
Postfix是眼下Linux下主流的邮件server,也就是MTA,主要用来实现SMTP协议,它能够兼容sendmail.而postfix也是为了改进sendmail而制作产生的. 通常来说.pos ...
- AES中几种加密模式的区别:ECB、CBC、CFB、OFB、CTR
AES: aes是基于数据块的加密方式,也就是说,每次处理的数据时一块(16字节),当数据不是16字节的倍数时填充,这就是所谓的分组密码(区别于基于比特位的流密码),16字节是分组长度 分组加密的几种 ...
- centos 7 下安装haproxy
1 haproxy 下载 从如下目录下载haproxy:http://www.haproxy.org/download/1.7/src/haproxy-1.7.1.tar.gz 2 haproxy 安 ...
- 通过主机名(域名)获取IP地址,主机别名等信息
一.所用API函数介绍 struct hostent FAR*gethostbyname( const char FAR* name ); 传入參数:const char FAR* name.主机名或 ...