gpt4 book ai didi

使用 Python 命令行进行调度

转载 作者:行者123 更新时间:2023-12-01 09:27:43 29 4
gpt4 key购买 nike

我有一个问题,我想自动化脚本。在过去的项目中,我为此使用了 python 调度程序。但对于这个项目我不确定如何处理这个问题。

问题在于,该代码使用代码之外的登录详细信息,并在启动脚本时在命令行中输入。

例如。 python scriptname.py email@youremail.com 密码

如何使用 python 调度程序自动执行此操作?“scriptname.py”中的代码是:

//LinkedBot.py
import argparse, os, time
import urlparse, random
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from bs4 import BeautifulSoup

def getPeopleLinks(page):
links = []
for link in page.find_all('a'):
url = link.get('href')
if url:
if 'profile/view?id=' in url:
links.append(url)
return links

def getJobLinks(page):
links = []
for link in page.find_all('a'):
url = link.get('href')
if url:
if '/jobs' in url:
links.append(url)
return links

def getID(url):
pUrl = urlparse.urlparse(url)
return urlparse.parse_qs(pUrl.query)['id'][0]


def ViewBot(browser):
visited = {}
pList = []
count = 0
while True:
#sleep to make sure everything loads, add random to make us look human.
time.sleep(random.uniform(3.5,6.9))
page = BeautifulSoup(browser.page_source)
people = getPeopleLinks(page)
if people:
for person in people:
ID = getID(person)
if ID not in visited:
pList.append(person)
visited[ID] = 1
if pList: #if there is people to look at look at them
person = pList.pop()
browser.get(person)
count += 1
else: #otherwise find people via the job pages
jobs = getJobLinks(page)
if jobs:
job = random.choice(jobs)
root = 'http://www.linkedin.com'
roots = 'https://www.linkedin.com'
if root not in job or roots not in job:
job = 'https://www.linkedin.com'+job
browser.get(job)
else:
print "I'm Lost Exiting"
break

#Output (Make option for this)
print "[+] "+browser.title+" Visited! \n("\
+str(count)+"/"+str(len(pList))+") Visited/Queue)"


def Main():
parser = argparse.ArgumentParser()
parser.add_argument("email", help="linkedin email")
parser.add_argument("password", help="linkedin password")
args = parser.parse_args()

browser = webdriver.Firefox()

browser.get("https://linkedin.com/uas/login")


emailElement = browser.find_element_by_id("session_key-login")
emailElement.send_keys(args.email)
passElement = browser.find_element_by_id("session_password-login")
passElement.send_keys(args.password)
passElement.submit()

在 OSX 上运行它。

最佳答案

我可以看到至少有两种不同的方式来自动触发脚本。既然您提到您的脚本是这样启动的:

python scriptname.py email@youremail.com password

这意味着您从 shell 启动它。当你想要安排它时,听起来 Crontab 是一个完美的答案。 (例如,参见https://kvz.io/blog/2007/07/29/schedule-tasks-on-linux-using-crontab/)

如果你确实想使用Python调度程序,你可以使用子进程。

在使用 python 调度程序的文件中:

import subprocess

subprocess.call("python scriptname.py email@youremail.com password", shell=True)

What is the best way to call a Python script from another Python script?

关于使用 Python 命令行进行调度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50232762/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com