广告位联系
返回顶部
分享到

爬取今日头条Ajax请求的教程

Ajax 来源:互联网搜集 作者:秩名 发布时间:2018-11-14 21:09:43 人浏览
摘要

今天小编给大家介绍爬取今日头条Ajax请求。 网址: https://www.toutiao.com/ 搜索头条 可以得到这个网址: https://www.toutiao.com/search/?keyword=%E8%A1%97%E6%8B%8D 开发者工具查看: 我们在搜索中并没有发现上面的文字,那么我们可以初步判定,这个由

今天小编给大家介绍爬取今日头条Ajax请求。

网址:

搜索头条

可以得到这个网址:



开发者工具查看:



我们在搜索中并没有发现上面的文字,那么我们可以初步判定,这个由Ajax加载,然后渲染出来的。此时切换到xhr过滤,可以看到确实是ajax请求。



观察请求的特点,发现只有offset是改变的,而且一次加20,。

我们可以用它来控制数据分页,然后把图片下载下来。代码如下:
 

import requests
import os
from urllib.parse import urlencode
from hashlib import md5
from multiprocessing.pool import Pool
from requests import codes
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:
      # print(url)
      return response.json()
  except requests.ConnectionError:
    return None
# get_page(0)
def get_images(json):
  if json.get('data'):
    for item in json.get('data'):
      if item.get('cell_type') is not None:
        continue
      title = item.get('title')
      images = item.get('image_list')
      for image in images:
        yield {
          'title':title,
          'image':'https:' + image.get('url'),
        }
def save_image(item):
  #os.path.sep  路径分隔符‘//'
  img_path = 'img' + os.path.sep + item.get('title')
  if not os.path.exists(img_path):
    os.makedirs(img_path)
  try:
    resp = requests.get(item.get('image'))
    # print(type(resp))
    if codes.ok == resp.status_code:
      file_path = img_path + os.path.sep + '{file_name}.{file_suffix}'.format(
        file_name=md5(resp.content).hexdigest(),#md5是一种加密算法获取图片的二进制数据,以二进制形式写入文件
        file_suffix='jpg')
      if not os.path.exists(file_path):
        with open(file_path,'wb')as f:
          f.write(resp.content)
          print('Downladed image path is %s' % file_path)
      else:
        print('Already Downloaded',file_path)
  except requests.ConnectionError:
    print('Failed to Save Image,item %s' % item)
def main(offset):
  json = get_page(offset)
  for item in get_images(json):
    print(item)
    save_image(item)
GROUP = 0
GROUP_END = 2
if __name__ == '__main__':
  pool = Pool()
  groups = ([x*20 for x in range(GROUP,GROUP_END)])
  pool.map(main,groups)  #将groups一个个调出来传给main函数
  pool.close()
  pool.join()   #保证子进程结束后再向下执行 pool.join(1) 等待一秒



版权声明 : 本文内容来源于互联网或用户自行发布贡献,该文观点仅代表原作者本人。本站仅提供信息存储空间服务和不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权, 违法违规的内容, 请发送邮件至2530232025#qq.cn(#换@)举报,一经查实,本站将立刻删除。
原文链接 :
相关文章
  • AJAX原理以及axios、fetch区别

    AJAX原理以及axios、fetch区别
    AJAX原理 Ajax的原理简单来说是在用户和服务器之间加了个中间层(AJAX引擎),通过XmlHttpRequest对象来向服务器发异步请求,从服务器获得数据,
  • Ajax实现上传图像功能

    Ajax实现上传图像功能
    最终效果展示 xhr发起请求 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
  • Ajax的疑难杂症介绍

    Ajax的疑难杂症介绍
    跨域问题 如图所示,这是通过jquery封装的ajax请求了一个本地的php文件(无框架),console提示CORS策略已阻止来自来源的null:请求的资源上不
  • 前端实现滑动按钮AJAX与后端交互的代码

    前端实现滑动按钮AJAX与后端交互的代码
    html代码 1 2 3 4 div class=switch-box input id=switchButton type=checkbox class=switch / label for=switchButton/label /div css代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1
  • AJAX实现省市县三级联动效果

    AJAX实现省市县三级联动效果
    (tips:其实省市县三级联动只需要引入jQuery省市县三级联动插件就可以实现) 效果图 首先准备两个服务端文件,另一个文件太长,这里就不
  • AJAX实现指定部分页面刷新效果的代码

    AJAX实现指定部分页面刷新效果的代码
    需求:点击左边的选项,不需要整个页面刷新,只进行右边页面的刷新。 这里需要三个文件 work.html !DOCTYPE htmlhtml lang=zh-CNhead meta charset=UTF
  • Ajax实现三级联动的代码

    Ajax实现三级联动的代码
    一、导入数据表和gson.jar 该表包括了中国所有的
  • AJAX实现注册验证用户名的代码
    功能说明 当用户在注册页面输入用户名并且鼠标焦点离开输入框时,到数据表中去验证该用户名是否已经存在,如果存在提示不可用,否则
  • react axios跨域访问一个或多个域名问题的详解
    1.react + axios 跨域访问一个域名 配置非常简单,只需要在当前的 package.json 文件里面配置: proxy:http://iot-demo-web-dev.autel.com, //当然,这里是一
  • $.ajax中contentType: “application/json” 的用法
    具体内容如下所示: $.ajax({ type: httpMethod, cache:false, async:false, contentType: application/json; charset=utf-8, dataType: json,//返回值类型 url: path+url, data:j
  • 本站所有内容来源于互联网或用户自行发布,本站仅提供信息存储空间服务,不拥有版权,不承担法律责任。如有侵犯您的权益,请您联系站长处理!
  • Copyright © 2017-2022 F11.CN All Rights Reserved. F11站长开发者网 版权所有 | 苏ICP备2022031554号-1 | 51LA统计