【问题标题】:Python - how to return to for loop using +=Python - 如何使用 += 返回 for 循环
【发布时间】:2019-05-01 01:31:48
【问题描述】:

我编写的 for 循环有问题,我无法让 for 循环返回到第一个 for 语句:

def output(query,page,max_page):
    """
    Parameters:
        query: a string
        max_page: maximum pages to be crawled per day, integer

    Returns:
    List of news dictionaries in a list: [[{...},{...}..],[{...},]]
    """
    news_dicts_all = []
    news_dicts = []
    # best to concatenate urls here
    date_range = get_dates()
    for date in get_dates():
        s_date = date.replace(".","")
        while page < max_page:
            url = "https://search.naver.com/search.naver?where=news&query=" + query + "&sort=0&ds=" + date + "&de=" + date + "&nso=so%3Ar%2Cp%3Afrom" + s_date + "to" + s_date + "%2Ca%3A&start=" + str(page)
            header = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'}
            req = requests.get(url,headers=header)
            cont = req.content
            soup = BeautifulSoup(cont, 'html.parser')
            for urls in soup.select("._sp_each_url"):
                try:
                    if urls["href"].startswith("https://news.naver.com"):
                        news_detail = get_news(urls["href"])
                        adict = dict()
                        adict["title"] = news_detail[0]
                        adict["date"] = news_detail[1]
                        adict["company"] = news_detail[3]
                        adict["text"] = news_detail[2]
                        news_dicts.append(adict)
                except Exception as e:
                    continue
            page += 10
        news_dicts_all.append(news_dicts)
    return news_dicts_all

我已经执行了代码,似乎page += 将代码返回到“while”部分,但在页面到达max_page 后不会返回到for date in get_dates() 部分。

我想要的本质上是在到达max_page 后返回到for date in get_dates() 的代码,但我不知道如何才能完成这项工作。

【问题讨论】:

    标签: python-3.x for-loop


    【解决方案1】:

    您永远不会重置 page,因此当它移动到 for 循环中的下一个日期时,page &gt; max_page 已经为真,因此它会完全跳过 while 循环。

    您需要执行一些操作,例如将您的 page 参数更改为 start_page,然后在 for 循环的开头使用 page = start_page

    【讨论】:

      猜你喜欢
      • 2015-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-26
      • 2013-10-13
      • 2019-07-31
      • 2021-12-17
      • 2019-11-23
      相关资源
      最近更新 更多