Skip to content

Commit

Permalink
'添加远程下载文件脚本,并调整目录结构'
Browse files Browse the repository at this point in the history
  • Loading branch information
cuizhongyi committed Aug 2, 2018
1 parent daa57c5 commit e0239ac
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 5 deletions.
File renamed without changes.
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
## 1、12306抢票脚本---qiangpiao.py

## 2、段友之家贴吧数据爬取---nhdz.py

## 3、百思不得姐网站图片数据爬取---bsbdj.py
## Python 小脚本,Python版本为3.6
File renamed without changes.
61 changes: 61 additions & 0 deletions download_remote/download_folder.py
Original file line number Diff line number Diff line change
@@ -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()
22 changes: 22 additions & 0 deletions json/json.py
Original file line number Diff line number Diff line change
@@ -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)
6 changes: 6 additions & 0 deletions json/json.txt
Original file line number Diff line number Diff line change
@@ -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
File renamed without changes.
15 changes: 15 additions & 0 deletions request/request.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit e0239ac

Please sign in to comment.