使用库 paramiko
安装方法:
pip3 install paramiko
paramiko 用于实现sftp的文件上传(文件下载等操作参见文档)
使用urllib 抓取页面信息
import paramiko import urllib.request def ssh_connect(_host, _username, _password,_port): try: _ssh_fd = paramiko.SSHClient() _ssh_fd.set_missing_host_key_policy(paramiko.AutoAddPolicy()) _ssh_fd.connect(_host, username=_username, password=_password,port=_port) except Exception as e: print('ssh ' + _username + "@" + _host + " " + str(e)) exit() return _ssh_fd def sftp_open(_ssh_fd): return _ssh_fd.open_sftp() def sftp_put(_sftp_fd, _put_from_path, _put_to_path): return _sftp_fd.put(_put_from_path, _put_to_path) def sftp_get(_sftp_fd, _get_from_path, _get_to_path): return _sftp_fd.get(_get_from_path, _get_to_path) def sftp_close(_sftp_fd): _sftp_fd.close() def ssh_close(_ssh_fd): _ssh_fd.close() def upload_file(_host, _username, _password,_port, _source, _target): sshd = ssh_connect(_host, _username, _password,_port) sftpd = sftp_open(sshd) try: sftp_put(sftpd, _source, _target) except Exception as e: print('ERROR: sftp_pusts -- ' + str(e)) sftp_close(sftpd) ssh_close(sshd) def get_page_str(_url): # 在此处添加对页面数据进行处理的代码 fp = urllib.request.urlopen(_url) mybytes = fp.read() mystr = mybytes.decode("utf8") fp.close() return mystr def write_file(_str, _file): print(_str) f = open(_file, 'w') f.write(_str) f.close() # sftp地址 HOST_NAME = '' # sftp端口 一般为22 HOST_PORT = 22 # sftp 用户名 HOST_USERNAME = '' # sftp 密码 HOST_PASSWORD = '' # 本地暂存文件地址 SOURCE_FILE = '' # 上传的目标文件地址 TARGET_FILE = '' # 抓取的目标地址 URL = 'http://www.cnblogs.com/' def process(): write_file(get_page_str(URL), SOURCE_FILE) upload_file(HOST_NAME, HOST_USERNAME, HOST_PASSWORD,HOST_PORT, SOURCE_FILE, TARGET_FILE) # 运行程序 process()
可以配合bash来定时执行达到实时更新的效果