linux启动简单python脚本

不点 阅读:619 2021-04-01 11:06:14 评论:0

1.python脚本

#!/usr/bin/python 
# -*- coding: utf-8 -*- 
import requests 
import logging 
import time 
""" 
weilai 
200200813 
解析商品详细页面 
""" 
# 第一步,创建一个logger 
logger = logging.getLogger() 
logger.setLevel(logging.INFO)   # Log等级总开关 
 
# 第二步,创建一个handler,用于写入日志文件 
logfile = './logs/crawler_' + time.strftime('%Y-%m-%d', time.localtime()) + '.log' 
fh = logging.FileHandler(logfile, mode='a', encoding="UTF-8") 
fh.setLevel(logging.INFO)  # 用于写到file的等级开关 
 
# 第三步,再创建一个handler,用于输出到控制台 
ch = logging.StreamHandler() 
ch.setLevel(logging.INFO)    # 输出到console的log等级的开关 
 
# 第四步,定义handler的输出格式 
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') 
fh.setFormatter(formatter) 
ch.setFormatter(formatter) 
 
# 第五步,将logger添加到handler里面 
logger.addHandler(fh) 
logger.addHandler(ch) 
 
def getProductInfo(a): 
    # header = { 
    #     'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36', 
    #     'token':'test-token-for-superuser' 
    #     } 
    # url = 'http://123.xx.xx.xxx:8022/yszc-service/all/account/getUserInfo' 
    # requests.adapters.DEFAULT_RETRIES = 10 
 
 
    # proxies = { 
    # # 'http': '192.168.2.101:8118' 
    #      } 
    # # url = 'https://www.aliexpress.com/item/Camera-Lens-Protection-Phone-Case-on-For-iPhone-11-Pro-Max-8-7-6-6s-Plus/4001039633127.html?scm=1007.26694.140541.0&scm_id=1007.26694.140541.0&scm-url=1007.26694.140541.0&pvid=8c8535a3-95ed-4271-a14b-7e17c4de32be&fromRankId=1766446&_t=fromRankId:1766446' 
    # req=requests.get(url,headers=header,proxies=proxies,timeout=5) 
    # # req=requests.get(url,headers=header,timeout=5) 
    # print(req.text) 
 
 
 
    header = { 
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'} 
    URL1 = "https://www.aliexpress.com/item/32946430734.html?spm=a2g0o.ams_97944.topranking.1.64adUYFqUYFqk4&scm=1007.26694.140541.0&scm_id=1007.26694.140541.0&scm-url=1007.26694.140541.0&pvid=e473e5f3-b2da-4b93-bda5-694e9ef0658f&fromRankId=24800&_t=fromRankId:24800" 
    URL2 = "https://www.aliexpress.com/item/4001064703276.html?spm=a2g0o.ams_97944.topranking.1.33f9RDINRDINiC&scm=1007.26694.140541.0&scm_id=1007.26694.140541.0&scm-url=1007.26694.140541.0&pvid=d13fb323-04aa-4c76-ac9a-59fe52c7b409&fromRankId=1791960&_t=fromRankId:1791960" 
    URL3 = "https://www.aliexpress.com/item/32980948859.html?spm=a2g0o.ams_97944.topranking.7.33f9RDINRDINiC&scm=1007.26694.140541.0&scm_id=1007.26694.140541.0&scm-url=1007.26694.140541.0&pvid=d13fb323-04aa-4c76-ac9a-59fe52c7b409&fromRankId=1791960&_t=fromRankId:1791960" 
    URL4 = "https://www.aliexpress.com/item/4000585676274.html?spm=a2g0o.ams_97944.topranking.2.4fc2Mn6IMn6I0c&scm=1007.26694.140541.0&scm_id=1007.26694.140541.0&scm-url=1007.26694.140541.0&pvid=22b89b07-2ddc-44ca-a770-4c58087cf8a8&fromRankId=3159115&_t=fromRankId:3159115" 
    URL5 = "https://www.aliexpress.com/item/4000563401073.html?spm=a2g0o.ams_97944.topranking.1.4f303h5z3h5zNb&scm=1007.26694.140541.0&scm_id=1007.26694.140541.0&scm-url=1007.26694.140541.0&pvid=3cd7a77a-baef-4c29-8562-ab31718aacfc&fromRankId=3153027&_t=fromRankId:3153027" 
    URL6 = "https://www.aliexpress.com/item/32595636496.html?spm=a2g0o.ams_97944.topranking.8.4f303h5z3h5zNb&scm=1007.26694.140541.0&scm_id=1007.26694.140541.0&scm-url=1007.26694.140541.0&pvid=3cd7a77a-baef-4c29-8562-ab31718aacfc&fromRankId=3153027&_t=fromRankId:3153027" 
    URL7 = "https://www.aliexpress.com/item/32793775351.html?spm=a2g0o.ams_97944.topranking.1.35fdOvpaOvpa4l&scm=1007.26694.140541.0&scm_id=1007.26694.140541.0&scm-url=1007.26694.140541.0&pvid=f9ba2610-6dc6-4704-ac91-d0ae4a90eace&fromRankId=1793385&_t=fromRankId:1793385" 
    URL8 = "https://www.aliexpress.com/item/32868448064.html?spm=a2g0o.ams_97944.topranking.4.35fdOvpaOvpa4l&scm=1007.26694.140541.0&scm_id=1007.26694.140541.0&scm-url=1007.26694.140541.0&pvid=f9ba2610-6dc6-4704-ac91-d0ae4a90eace&fromRankId=1793385&_t=fromRankId:1793385" 
    URL9 = "https://www.aliexpress.com/item/4000776415795.html?spm=a2g0o.ams_97944.topranking.9.29c3O1DIO1DINB&scm=1007.26694.140541.0&scm_id=1007.26694.140541.0&scm-url=1007.26694.140541.0&pvid=ee4c55a9-51b8-41b6-be06-1586ee30ef91&fromRankId=1766078&_t=fromRankId:1766078" 
    URL10 = "https://www.aliexpress.com/item/4001110195225.html?spm=a2g0o.ams_97944.topranking.2.311b01fM01fMcg&scm=1007.26694.140541.0&scm_id=1007.26694.140541.0&scm-url=1007.26694.140541.0&pvid=4c84d8ee-7e5f-4ee1-8d44-8b05e5ed1166&fromRankId=2050963&_t=fromRankId:2050963" 
    requests.adapters.DEFAULT_RETRIES = 5 
    r = requests.get(URL1, headers=header) 
    a+=1 
    logger.info('a=='+str(a)+',status=='+str(r.status_code)) 
    r = requests.get(URL2, headers=header) 
    a+=1 
    logger.info('a=='+str(a)+',status=='+str(r.status_code)) 
    r = requests.get(URL3, headers=header) 
    a+=1 
    logger.info('a=='+str(a)+',status=='+str(r.status_code)) 
    r = requests.get(URL4, headers=header) 
    a+=1 
    logger.info('a=='+str(a)+',status=='+str(r.status_code)) 
    r = requests.get(URL5, headers=header) 
    a+=1 
    logger.info('a=='+str(a)+',status=='+str(r.status_code)) 
    r = requests.get(URL6, headers=header) 
    a+=1 
    logger.info('a=='+str(a)+',status=='+str(r.status_code)) 
    r = requests.get(URL7, headers=header) 
    a+=1 
    logger.info('a=='+str(a)+',status=='+str(r.status_code)) 
    r = requests.get(URL8, headers=header) 
    a+=1 
    logger.info('a=='+str(a)+',status=='+str(r.status_code)) 
    r = requests.get(URL9, headers=header) 
    a+=1 
    logger.info('a=='+str(a)+',status=='+str(r.status_code)) 
    r = requests.get(URL10, headers=header) 
    a+=1 
    logger.info('a=='+str(a)+',status=='+str(r.status_code)) 
    return a 
 
if __name__ == '__main__': 
    a =0 
    while True: 
        a = getProductInfo(a) 
 

2.启动脚本start.sh

nohup python testProxy.py >/dev/null 2>&1 & 

3.停止脚本shutdown.sh

ps aux|grep testProxy.py|grep -v grep|awk '{print $2}'|xargs kill -9 

4.重启脚本restart.sh

sh ./shutdown.sh 
echo shutdown ...  
sh ./start.sh 
echo start ...  
标签:Python
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

关注我们

一个IT知识分享的公众号