首页 > 解决方案 > Python:运行所有迭代,然后在循环内继续执行新任务

问题描述

我想知道是否有任何方便的方法可以将 while 循环中的所有迭代运行到循环中的某一行。我会用它来比较每次迭代的结果,并根据结果继续前进。

这是我到目前为止的代码:

source = requests.get('https://website.com').text
soup = BeautifulSoup(source, 'lxml')
pm = soup.find_all('div', class_='card-item span3')

antal_pm = [0, 1, 2, 3, 4]
d={}

for x in antal_pm:
    senaste = pm[x]
    senaste_text = senaste.text
    d["senaste_text{0}".format(x)] = senaste_text
    url_link = senaste.find('a').get('href')
    source = requests.get(url_link).text
    soup1 = BeautifulSoup(source, 'lxml')
    twitter_url = soup1.find('a', class_='twitter social-link').get('href')
    source1 = requests.get(twitter_url).text
    soup2 = BeautifulSoup(source1, 'lxml')
    twitter_grund = soup2.find('textarea').text
    url_link_formaterad = re.search('com/se/(.+?)/r/', url_link).group(1)
    d["ulf{0}".format(x)] = url_link_formaterad
    matches_grund = next((x for x in bolag if x in senaste_text), 'URL-triggad')
    d["match{0}".format(x)] = matches_grund
    twitter_p = twitter_grund[:-18] + ("#hashtag") + (" #anotherhashtag") + " " + (T1 if B1 in matches_grund else '') + (T2 if B2 in matches_grund else '') + (T3 if B3 in matches_grund else '') + (T4 if B4 in matches_grund else '') + (T5 if B5 in matches_grund else '') + (T6 if B6 in matches_grund else '') + (T7 if B7 in matches_grund else '') + (T8 if B8 in matches_grund else '') + (T9 if B9 in matches_grund else '') + (T10 if B10 in matches_grund else '') + (T11 if B11 in matches_grund else '') + (T12 if B12 in matches_grund else '') + (T13 if B13 in matches_grund else '') + (T14 if B14 in matches_grund else '') + (T15 if B15 in matches_grund else '') + (T16 if B16 in matches_grund else '') + (T17 if B17 in matches_grund else '') + (T18 if B18 in matches_grund else '') + (T19 if B19 in matches_grund else '') + (T20 if B20 in matches_grund else '') + (T21 if B21 in matches_grund else '') + (T22 if B22 in matches_grund else '') + (T23 if B23 in matches_grund else '') + (T24 if B24 in matches_grund else '') + (T25 if B25 in matches_grund else '') + (T26 if B26 in matches_grund else '') + (T27 if B27 in matches_grund else '') + (T28 if B28 in matches_grund else '') + (T29 if B29 in matches_grund else '') + (T30 if B30 in matches_grund else '') + (T31 if B31 in matches_grund else '') + (T32 if B32 in matches_grund else '') + (T33 if B33 in matches_grund else '') + (T34 if B34 in matches_grund else '') + (T35 if B35 in matches_grund else '') + (T36 if B36 in matches_grund else '') + (T37 if B37 in matches_grund else '') + (T38 if B38 in matches_grund else '') + (T39 if B39 in matches_grund else '')# + (T40 if B40 in matches1 else '')# + (T41 if B41 in matches1 else '') + (T42 if B42 in matches1 else '') + (T43 if B43 in matches1 else '') + (T44 if B44 in matches1 else '') + (T45 if B45 in matches1 else '') + (T46 if B46 in matches1 else '') + (T47 if B47 in matches1 else '') + (T48 if B48 in matches1 else '') + (T49 if B49 in matches1 else '') + (T50 if B50 in matches1 else '') + (T51 if B51 in matches1 else '') + (T52 if B52 in matches1 else '') + (T53 if B53 in matches1 else '') + (T54 if B54 in matches1 else '') + (T55 if B55 in matches1 else '') + (T56 if B56 in matches1 else '') + (T57 if B57 in matches1 else '') + (T58 if B58 in matches1 else '') + (T59 if B59 in matches1 else '') + (T60 if B60 in matches1 else '') + (T61 if B61 in matches1 else '') + (T62 if B62 in matches1 else '') + (T63 if B63 in matches1 else '') + (T64 if B64 in matches1 else '') + (T65 if B65 in matches1 else '') + (T66 if B66 in matches1 else '') + (T67 if B67 in matches1 else '') + (T68 if B68 in matches1 else '') + (T69 if B69 in matches1 else '') + (T70 if B70 in matches1 else '') + (T71 if B71 in matches1 else '') + (T72 if B72 in matches1 else '') + (T73 if B73 in matches1 else '') + (T74 if B74 in matches1 else '') + (T75 if B75 in matches1 else '')
    d["twitter_post{0}".format(x)] = twitter_p
    title_base = re.search('(.+?) https://', twitter_p).group(1)[:100]
    d["title{0}".format(x)] = title_base

我想通过这段代码运行所有 10 次迭代,然后能够总结结果并基于它继续前进。

关于循环如何继续的示例:

    all_titles = d[title0] + " " + d[title1] + " " + d[title2] + " " + d[title3] + " " + d[title4]

    if matches_grund in all_titles:
        print("found")
        post_through_api()
`````Note that ```` matches_grund ```` will be different for each iteration. Thanks in advance!

标签: pythonloopswhile-loop

解决方案


推荐阅读