Giter VIP home page Giter VIP logo

demo-python-requests-webpage-json-with-image's Introduction

demo-python-requests-webpage-json-with-image

1开发环境配置

  1. 安装Python3.0+
  2. 安装Pycharm
  3. 在Pycharm实现以下代码

2代码

import requests, os
path_a = os.path.abspath('.')
kw = ''
while True:
   kw = input('请输入你要获取的图片(若想结束请输入1)')
   if kw == '1':
   print('已退出,你下载的图片已保存在'+path_a+',请查看!')
   break
   for x in range(0, 1, 5):
   url = 'https://www.toutiao.com/search_content/?offset='+str(x)+'&format=json&keyword=%s&autoload=true&count=20&cur_tab=3&from=gallery' % kw
   response = requests.get(url)
   data = response.json()['data']
   if not data:
   print('下载'+kw+'图片完毕,请换个关键词继续')
   break
   n = 1  # 记录文章数
   for atlas in data:
   # 创建目录
   title = atlas['title']
   print(atlas)
   try:
   if title not in os.listdir('.'):  # 防止文件名已经存在
   os.mkdir(title)
   except OSError as e:
   print('文件名出错,创建目录失败,重新创建一个随机名字')
   title = kw + '文件名出错'+str(x)
   if title not in os.listdir('.'):
   os.mkdir(title)
   k = 1  # 记录下载的图片数
   path = os.path.join(path_a, title)
   # 转进图片目录
   os.chdir(path)
   for image in atlas['image_list']:  # 这个链接获取的图片是小张的,看着不够爽,所以下面替换成大的图片
   image_url = image['url'].replace('list', 'large')  # 改个链接获取大的图片
   atlas = requests.get('http:'+image_url).content
   with open(str(k)+'.jpg', 'wb') as f:  # 把图片写入文件内
   f.write(atlas)
   print('下载完第%d个文章的%d幅图完成' % (x+n, k))
   k += 1
   n += 1
   # 转出图片目录
   os.chdir(path_a)

3选择环境Python环境

  1. 使用Ctrl+Alt+S快捷键
  2. 选择Project:xxx.py下拉菜单
  3. 选择Project Interpreter
  4. 在Project Interpreter下拉菜单下选择Show All...
  5. 选择Python3.6+
  6. Apply ok

4运行

结果:请输入你要获取的图片(若想结束请输入1)

在Pycharm输入你想要爬取的图片

爬取结果会自动生成在你.py文件下

demo-python-requests-webpage-json-with-image's People

Contributors

lonch-wong avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.