From e0239ac3091a825ecc7e0267535a74405178c672 Mon Sep 17 00:00:00 2001 From: cuizhongyi Date: Thu, 2 Aug 2018 14:47:46 +0800 Subject: [PATCH] =?UTF-8?q?'=E6=B7=BB=E5=8A=A0=E8=BF=9C=E7=A8=8B=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD=E6=96=87=E4=BB=B6=E8=84=9A=E6=9C=AC=EF=BC=8C=E5=B9=B6?= =?UTF-8?q?=E8=B0=83=E6=95=B4=E7=9B=AE=E5=BD=95=E7=BB=93=E6=9E=84'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- qiangpiao.py => 12306qiangpiao/qiangpiao.py | 0 README.md | 6 +- bsbdj.py => bsbdj/bsbdj.py | 0 download_remote/download_folder.py | 61 +++++++++++++++++++++ json/json.py | 22 ++++++++ json/json.txt | 6 ++ nhdz.py => nhdz/nhdz.py | 0 request/request.py | 15 +++++ 8 files changed, 105 insertions(+), 5 deletions(-) rename qiangpiao.py => 12306qiangpiao/qiangpiao.py (100%) rename bsbdj.py => bsbdj/bsbdj.py (100%) create mode 100644 download_remote/download_folder.py create mode 100644 json/json.py create mode 100644 json/json.txt rename nhdz.py => nhdz/nhdz.py (100%) create mode 100644 request/request.py diff --git a/qiangpiao.py b/12306qiangpiao/qiangpiao.py similarity index 100% rename from qiangpiao.py rename to 12306qiangpiao/qiangpiao.py diff --git a/README.md b/README.md index b05bf50..72e4b74 100644 --- a/README.md +++ b/README.md @@ -1,5 +1 @@ -## 1、12306抢票脚本---qiangpiao.py - -## 2、段友之家贴吧数据爬取---nhdz.py - -## 3、百思不得姐网站图片数据爬取---bsbdj.py \ No newline at end of file +## Python 小脚本,Python版本为3.6 \ No newline at end of file diff --git a/bsbdj.py b/bsbdj/bsbdj.py similarity index 100% rename from bsbdj.py rename to bsbdj/bsbdj.py diff --git a/download_remote/download_folder.py b/download_remote/download_folder.py new file mode 100644 index 0000000..b2ced40 --- /dev/null +++ b/download_remote/download_folder.py @@ -0,0 +1,61 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +""" +通过paramiko从远处服务器下载文件资源到本地 +author: gxcuizy +time: 2018-08-01 +""" + +import paramiko +import os +from stat import S_ISDIR as isdir + + +def down_from_remote(sftp_obj, remote_dir_name, local_dir_name): + """远程下载文件""" + remote_file = sftp_obj.stat(remote_dir_name) + if isdir(remote_file.st_mode): + # 文件夹,不能直接下载,需要继续循环 + check_local_dir(local_dir_name) + print('开始下载文件夹:' + remote_dir_name) + for remote_file_name in sftp.listdir(remote_dir_name): + sub_remote = os.path.join(remote_dir_name, remote_file_name) + sub_remote = sub_remote.replace('\\', '/') + sub_local = os.path.join(local_dir_name, remote_file_name) + sub_local = sub_local.replace('\\', '/') + down_from_remote(sftp_obj, sub_remote, sub_local) + else: + # 文件,直接下载 + print('开始下载文件:' + remote_dir_name) + sftp.get(remote_dir_name, local_dir_name) + + +def check_local_dir(local_dir_name): + """本地文件夹是否存在,不存在则创建""" + if not os.path.exists(local_dir_name): + os.makedirs(local_dir_name) + + +if __name__ == "__main__": + """程序主入口""" + # 服务器连接信息 + host_name = '172.17.2.18' + user_name = 'dev' + password = 'dev@zdlh' + port = 22 + # 远程文件路径(需要绝对路径) + remote_dir = '/data/nfs/zdlh/pdf/2018/07/31' + # 本地文件存放路径(绝对路径或者相对路径都可以) + local_dir = 'file_download/' + + # 连接远程服务器 + t = paramiko.Transport((host_name, port)) + t.connect(username=user_name, password=password) + sftp = paramiko.SFTPClient.from_transport(t) + + # 远程文件开始下载 + down_from_remote(sftp, remote_dir, local_dir) + + # 关闭连接 + t.close() diff --git a/json/json.py b/json/json.py new file mode 100644 index 0000000..9c0a6e9 --- /dev/null +++ b/json/json.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import json + +# 程序主入口 +if __name__ == "__main__": + """解读字符串拼装成json写入文件,方便其他语言解析数据""" + file_path = 'json.txt' + file_obj = open(file_path, "r", encoding='UTF-8') + all_lines = file_obj.readlines() + data = {} + key_value = 0 + for line in all_lines: + line_info = line.split(',') + if line_info: + info = {"name": line_info[0], "url": line_info[1].replace("\n", "")} + data[key_value] = info + key_value += 1 + file_obj.close() + file = open('json.json', 'w', encoding='utf-8') + json.dump(data, file, ensure_ascii=False) diff --git a/json/json.txt b/json/json.txt new file mode 100644 index 0000000..7669b5c --- /dev/null +++ b/json/json.txt @@ -0,0 +1,6 @@ +111111,http://www.baidu.com +222222,http://www.sina.com +333333,http://www.google.com +444444,http://www.163.com +555555,http://www.qq.com +666666,http://www.y0701.com diff --git a/nhdz.py b/nhdz/nhdz.py similarity index 100% rename from nhdz.py rename to nhdz/nhdz.py diff --git a/request/request.py b/request/request.py new file mode 100644 index 0000000..7987eb7 --- /dev/null +++ b/request/request.py @@ -0,0 +1,15 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import requests + +# 程序主入口 +if __name__ == "__main__": + """模仿浏览器,请求api信息""" + url = 'http://xssychina.com/plus/count.php?view=yes&aid=206&mid=1' + headers = { + 'User-Agent': 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6' + } + request = requests.get(url, headers=headers) + html_text = request.text + print(html_text)