"""
author:张鑫
date:2021/10/14 17:14
"""
import time
import paramiko
import requests
from lxml import etree
from selenium import webdriver
keyword = 'SandyHsu312'
ir_urltime = time.time()
ip = "140.210.4.73" # 服务器ip
port = 22 # 端口号
username = "zs_caoye" # 用户名
password = "zs_caoye#@!s" # 密码
img_name = keyword + '_' + str(ir_urltime) + '.jpg'
# 本地资源位置
local = fr'C:\Users\li9\Desktop\facebook\网红\{img_name}'
# 服务器位置
remote = f'/home/wwwroot/tw_ipadsvn/resource/assets/upload/img/cjimg/{img_name}'
def uploadfiletoserver(local, remote, img_name):
# 创建SSH对象
ssh = paramiko.SSHClient()
# 允许连接不在know_hosts文件中的主机
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip, port, username, password)
sftp = ssh.open_sftp()
sftp.put(local, remote)
print('上传成功')
# 视频链接
# 调用接口
video_link = f'http://twipad.hnxinxiudata.top/resource/assets/upload/img/cjimg/{img_name}'
print('图片接口', video_link)
url = f'https://www.facebook.com/{keyword}'
driver = webdriver.Chrome()
driver.get(url)
html = driver.page_source
tree = etree.HTML(html)
try:
ir_picture = tree.xpath(
'//img[@class="i09qtzwb n7fi1qx3 datstx6m pmk7jnqg j9ispegn kr520xx4 k4urcfbm bixrwtb6"]//@src'
)
if ir_picture == []:
ir_picture = tree.xpath(
'//img[@class="i09qtzwb n7fi1qx3 datstx6m pmk7jnqg j9ispegn kr520xx4 k4urcfbm"]//@src'
)
if ir_picture == []:
ir_picture = tree.xpath(
'//div[@class="pmk7jnqg kr520xx4"]/img//@src'
)
# print(f'图片集合:{ir_picture}')
try:
if ir_picture == []:
ir_imgurl2 = ''
img_url = ''
else:
ir_imgurl2 = []
if '?' in keyword:
keyword = keyword.replace('?', '')
img_name = keyword + '_' + str(ir_urltime) + '.jpg'
for img_url in ir_picture:
ir_imgurl2.append(f'http://twipad.hnxinxiudata.top/resource/assets/upload/img/cjimg/{img_name}')
print(f'图片链接:{img_url}')
# ir_imgurl2=''.join(ir_imgurl2)
print(f'图片接口:{ir_imgurl2}')
if img_url == '':
pass
else:
def download_img(img_url, img_name):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36'
}
r = requests.get(img_url, headers=headers, stream=True)
# print(r.status_code) # 返回状态码
if r.status_code == 200:
# 截取图片文件名
with open(fr'D:\孔子学院\facebook\网红\{img_name}', 'wb') as f:
f.write(r.content)
return img_name
img_name = download_img(img_url, img_name)
# 本地资源位置
local = fr'D:\孔子学院\facebook\网红\{img_name}'
# 服务器位置
remote = f'/home/wwwroot/tw_ipadsvn/resource/assets/upload/img/cjimg/{img_name}'
uploadfiletoserver(local, remote, img_name)
# delete_img(img_name)
except Exception as e:
print(f'*****************错误信息1:{e}*********************')
# ir_imgurl2=''
pass
except Exception as e:
print(f'*****************错误信息2:{e}*********************')
pass