python爬取豆瓣图片到本地,并用java上传至服务

换行缩进不要用tab键!!!!

import requests
import urllib
import json
import os,sys
from lxml import etree

def main():
    f = open('test1.txt', 'w')
    path = os.getcwd()+'/image';
    print(path)
    if not os.path.isdir(path):
        os.makedirs(path)
    for i in range(1):
        url = 'https://movie.douban.com/j/search_subjects?type=tv&tag=%E7%83%AD%E9%97%A8&sort=recommend&page_limit=20&page_start='+str(i*20)
        print(url)
        data = requests.get(url)
        res = json.loads(data.text)
        result = []
        for x in res["subjects"]:
            result.append(x["cover"])
        for i in range(0, len(result)):
            f.write(result[i]+'\n')
            filename = result[i].strip().split('/')[-1]
            print(result[i])
			#urllib.request.urlretrieve(result[i], filename)
            imagepath = path+"/"+filename
            response = urllib.urlopen(result[i])
            cat_img = response.read()
            with open(imagepath, 'wb') as tag:
             tag.write(cat_img)

if __name__ == '__main__':
	main()

数据从豆瓣的接口中获取的,并非是返回的网页中通过正则获取。

 url = 'https://movie.douban.com/j/search_subjects?type=tv&tag=%E7%83%AD%E9%97%A8&sort=recommend&page_limit=20&page_start='+str(i*20)

因此,把返回的数据进行json化,然后获取其中的list.

  data = requests.get(url)
  res = json.loads(data.text)

在这里插入图片描述
循环获取subjects中的元素的cover,写入到result中。

 result = []
 for x in res["subjects"]:
     result.append(x["cover"])

第一层for循环是为了改变页码,获取更多信息。

1.打开文件,写入数据

 f = open('test1.txt', 'w')

2.文件存放路径

path = os.getcwd()+'/image';
if not os.path.isdir(path):
   os.makedirs(path)

3.获取爬去链接的后面的名字

filename = result[i].strip().split('/')[-1]

4.下载文件到本地

 response = urllib.urlopen(result[i])
 cat_img = response.read()
 with open(imagepath, 'wb') as tag:
     tag.write(cat_img)

java 把文件上传到服务器

 @GetMapping("/uploadImage")
    public Object uploadFile() throws Exception {
        String dirPath = "/Users/davi/Documents/GitHub/python/image";
        File dir = new File(dirPath);
        File[] files = dir.listFiles(); // 该文件目录下文件全部放入数组
        String type = "resource";

        final FileType fileType = FileType.valueOf(type.toUpperCase());

        FileWriter fw = new FileWriter("/Users/davi/Desktop/imageout.txt");
        for (int i = 0; i < files.length; i++) {
            if (files[i].isFile()) { // 判断是文件还是文件夹
                File file = files[i];
                FileInputStream fileInputStream = new FileInputStream(file);
                //个人的上传服务接口
                final MyFileCreateRequest createFileRequest = new MyFileCreateRequest(fileType, file.getName(), "multipart/form-data; boundary=----WebKitFormBoundaryNKE6Mjn6vK5PxlhK", fileInputStream);
                final MyFileInfoVo result = this.myservice.createFile(createFileRequest);
                System.out.println("upload image result:" + result.getUrl());
                fw.write(result.getUrl()+"\n");
            }
        }
        fw.close();
        return Results.singleResult(ResultCodes.SUCCESS);
    }

爬取的图片
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值