Измените истинный скрипт Python, чтобы он запускался только один раз - PullRequest
0 голосов
/ 26 февраля 2019

Я новичок в python и хочу, чтобы этот код запускался только один раз и останавливался, а не каждые 30 секунд

, потому что я хочу запускать несколько таких кодов с разными токенами доступа каждые 5 секунд с помощью командылиния.и когда я попробовал этот код, он никогда не переходит на второй, потому что это правда:

import requests
import time

api_url = "https://graph.facebook.com/v2.9/"
access_token = "access token"
graph_url = "site url"
post_data = { 'id':graph_url, 'scrape':True, 'access_token':access_token }
# Beware of rate limiting if trying to increase frequency.
refresh_rate = 30 # refresh rate in second

while True:
    try:
        resp = requests.post(api_url, data = post_data)
        if resp.status_code == 200:
            contents = resp.json()
            print(contents['title'])
        else:
            error = "Warning: Status Code {}\n{}\n".format(
                resp.status_code, resp.content)
            print(error)
            raise RuntimeWarning(error)
    except Exception as e:
        f = open ("open_graph_refresher.log", "a")
        f.write("{} : {}".format(type(e), e))
        f.close()
        print(e)
    time.sleep(refresh_rate)

1 Ответ

0 голосов
/ 26 февраля 2019

Из того, что я понял, вы пытаетесь выполнить фрагмент кода для маркеров множественного доступа.Чтобы упростить вашу работу, укажите все свои access_tokens в виде списков и используйте следующий код.Предполагается, что вы знаете все свои access_tokens заранее.

import requests
import time

def scrape_facebook(api_url, access_token, graph_url):
    """ Scrapes the given access token"""
    post_data = { 'id':graph_url, 'scrape':True, 'access_token':access_token }

    try:
        resp = requests.post(api_url, data = post_data)
        if resp.status_code == 200:
            contents = resp.json()
            print(contents['title'])
        else:
            error = "Warning: Status Code {}\n{}\n".format(
                resp.status_code, resp.content)
            print(error)
            raise RuntimeWarning(error)
    except Exception as e:
        f = open (access_token+"_"+"open_graph_refresher.log", "a")
        f.write("{} : {}".format(type(e), e))
        f.close()
        print(e)

access_token = ['a','b','c']
graph_url = ['sss','xxx','ppp']
api_url = "https://graph.facebook.com/v2.9/"

for n in range(len(graph_url)):
    scrape_facebook(api_url, access_token[n], graph_url[n])
    time.sleep(5)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...