循环以下代码,直到if语句为True

时间:2016-12-22 05:26:54

标签: python loops web-scraping beautifulsoup flow

在找到正确的关键字之前无法循环此代码,只有这样代码才能继续执行注释行...到目前为止,此代码将继续通过注释行,无论是否找到给定的关键字。

from bs4 import BeautifulSoup
import requests
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import urllib2
import time
from lxml import etree
while True:
    keyword1 = "example"
    keyword2 = "stackoverflow"
    keyword3 = "notactuallyonwebsite"
    print("starting")
    r = requests.get('http://kithnyc.com/sitemap_products_1.xml?from=60594372&to=9545825095')
    soup = BeautifulSoup(r.text, 'lxml')
    links = soup.find_all('loc')
    for link in links:
        if keyword1 in link.text and keyword2 in link.text and keyword3 in link.text:
            logic = True
        if logic == True:
            continue
    #################################### comment line ####
    print(link.text)
    jake = str(link.text)
    print(jake + "link scraped")

1 个答案:

答案 0 :(得分:0)

continue适用于内环,而不适用于外环。此外,您不需要设置变量然后检查它;只是检查条件本身。

links = soup.find_all('loc')
if all(keyword1 in link.text and keyword2 in link.text for link in links):
    continue
#################################### comment line ####