Skip to content

Commit e0239ac

Browse files
author
cuizhongyi
committed
'添加远程下载文件脚本,并调整目录结构'
1 parent daa57c5 commit e0239ac

File tree

8 files changed

+105
-5
lines changed

8 files changed

+105
-5
lines changed
File renamed without changes.

README.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1 @@
1-
## 1、12306抢票脚本---qiangpiao.py
2-
3-
## 2、段友之家贴吧数据爬取---nhdz.py
4-
5-
## 3、百思不得姐网站图片数据爬取---bsbdj.py
1+
## Python 小脚本,Python版本为3.6

bsbdj.py renamed to bsbdj/bsbdj.py

File renamed without changes.

download_remote/download_folder.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
"""
5+
通过paramiko从远处服务器下载文件资源到本地
6+
author: gxcuizy
7+
time: 2018-08-01
8+
"""
9+
10+
import paramiko
11+
import os
12+
from stat import S_ISDIR as isdir
13+
14+
15+
def down_from_remote(sftp_obj, remote_dir_name, local_dir_name):
16+
"""远程下载文件"""
17+
remote_file = sftp_obj.stat(remote_dir_name)
18+
if isdir(remote_file.st_mode):
19+
# 文件夹,不能直接下载,需要继续循环
20+
check_local_dir(local_dir_name)
21+
print('开始下载文件夹:' + remote_dir_name)
22+
for remote_file_name in sftp.listdir(remote_dir_name):
23+
sub_remote = os.path.join(remote_dir_name, remote_file_name)
24+
sub_remote = sub_remote.replace('\\', '/')
25+
sub_local = os.path.join(local_dir_name, remote_file_name)
26+
sub_local = sub_local.replace('\\', '/')
27+
down_from_remote(sftp_obj, sub_remote, sub_local)
28+
else:
29+
# 文件,直接下载
30+
print('开始下载文件:' + remote_dir_name)
31+
sftp.get(remote_dir_name, local_dir_name)
32+
33+
34+
def check_local_dir(local_dir_name):
35+
"""本地文件夹是否存在,不存在则创建"""
36+
if not os.path.exists(local_dir_name):
37+
os.makedirs(local_dir_name)
38+
39+
40+
if __name__ == "__main__":
41+
"""程序主入口"""
42+
# 服务器连接信息
43+
host_name = '172.17.2.18'
44+
user_name = 'dev'
45+
password = 'dev@zdlh'
46+
port = 22
47+
# 远程文件路径(需要绝对路径)
48+
remote_dir = '/data/nfs/zdlh/pdf/2018/07/31'
49+
# 本地文件存放路径(绝对路径或者相对路径都可以)
50+
local_dir = 'file_download/'
51+
52+
# 连接远程服务器
53+
t = paramiko.Transport((host_name, port))
54+
t.connect(username=user_name, password=password)
55+
sftp = paramiko.SFTPClient.from_transport(t)
56+
57+
# 远程文件开始下载
58+
down_from_remote(sftp, remote_dir, local_dir)
59+
60+
# 关闭连接
61+
t.close()

json/json.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
import json
5+
6+
# 程序主入口
7+
if __name__ == "__main__":
8+
"""解读字符串拼装成json写入文件,方便其他语言解析数据"""
9+
file_path = 'json.txt'
10+
file_obj = open(file_path, "r", encoding='UTF-8')
11+
all_lines = file_obj.readlines()
12+
data = {}
13+
key_value = 0
14+
for line in all_lines:
15+
line_info = line.split(',')
16+
if line_info:
17+
info = {"name": line_info[0], "url": line_info[1].replace("\n", "")}
18+
data[key_value] = info
19+
key_value += 1
20+
file_obj.close()
21+
file = open('json.json', 'w', encoding='utf-8')
22+
json.dump(data, file, ensure_ascii=False)

json/json.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
111111,http://www.baidu.com
2+
222222,http://www.sina.com
3+
333333,http://www.google.com
4+
444444,http://www.163.com
5+
555555,http://www.qq.com
6+
666666,http://www.y0701.com

nhdz.py renamed to nhdz/nhdz.py

File renamed without changes.

request/request.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
import requests
5+
6+
# 程序主入口
7+
if __name__ == "__main__":
8+
"""模仿浏览器,请求api信息"""
9+
url = 'http://xssychina.com/plus/count.php?view=yes&aid=206&mid=1'
10+
headers = {
11+
'User-Agent': 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'
12+
}
13+
request = requests.get(url, headers=headers)
14+
html_text = request.text
15+
print(html_text)

0 commit comments

Comments
 (0)