为一段非简单的代码创建一个while循环

时间:2018-07-25 23:01:53

标签: python web-scraping while-loop

我是Python(和一般编程领域)的新手,在创建while循环时遇到了麻烦。我能够找到的所有示例都非常简单,沿着var = 10的行,减去1直到达到0。只要条件满足,我就有一段较大的代码需要重新运行见过,但是我尝试过的没有成功。我在Anaconda中使用Python 3。

我已经尝试了def main()和下面的命令,现在我正在尝试true(在我的代码示例中,我在while的下面有true,但缩进了,但在这里似乎无法缩进) 。我要尝试执行的操作是检查datey是否小于endDate,如果是,则重新开始整个操作。

这就是我所拥有的:

from openpyxl import load_workbook
while True:

    WB = load_workbook('File.xlsx', data_only=True)
    SH = WB['02474']
    import datetime
    from datetime import timedelta
    endDate = datetime.datetime.strptime('2018-02-09', '%Y-%m-%d')

    MXrow = SH.max_row
    NXrow = MXrow+1
    datex = SH.cell(row=MXrow, column=2)
    datex2 = datex.value
    datey = datetime.datetime.strptime(datex2, '%Y-%m-%d') + datetime.timedelta(days=1)
    datey2 = datey.strftime('%Y-%m-%d')
    URL = 'https://www.example.com' + str(datey2)

    import requests
    page = requests.get(URL)
    from bs4 import BeautifulSoup
    soup = BeautifulSoup(page.text, 'lxml')

    mintemp = soup.find("tr",{"class":"weatherhistory_results_datavalue temp_mn"})
    for i in mintemp:
        mintemp2 = i.find("span",{"class": "value"})

    maxtemp = soup.find("tr",{"class":"weatherhistory_results_datavalue temp_mx"})
    for i in maxtemp:
        maxtemp2 = i.find("span",{"class": "value"})

    mintemp3 = mintemp2.text
    maxtemp3 = maxtemp2.text

    while(datey <= endDate):
        SH.cell(row=NXrow, column=1).value = "ZIP"
        SH.cell(row=NXrow, column=2).value = datey2
        SH.cell(row=NXrow, column=3).value = URL
        SH.cell(row=NXrow, column=4).value = mintemp3
        SH.cell(row=NXrow, column=5).value = maxtemp3
        WB.save('File.xlsx')

希望您能给我提供任何解决方案的指针!

1 个答案:

答案 0 :(得分:0)

如果要检查

  

datey小于endDate

然后只需在顶部使用while循环参数即可,而不是布尔值:

import requests
import datetime
from datetime import timedelta

while(datey <= endDate):

    WB = load_workbook('File.xlsx', data_only=True)

    SH = WB['02474']
    endDate = datetime.datetime.strptime('2018-02-09', '%Y-%m-%d')

    MXrow = SH.max_row
    NXrow = MXrow+1
    datex = SH.cell(row=MXrow, column=2)
    datex2 = datex.value
    datey = datetime.datetime.strptime(datex2, '%Y-%m-%d') + 
    datetime.timedelta(days=1)
    datey2 = datey.strftime('%Y-%m-%d')
    URL = 'https://www.example.com' + str(datey2)


    page = requests.get(URL)
    from bs4 import BeautifulSoup
    soup = BeautifulSoup(page.text, 'lxml')

    mintemp = soup.find("tr",{"class":"weatherhistory_results_datavalue temp_mn"})
    for i in mintemp:
        mintemp2 = i.find("span",{"class": "value"})

    maxtemp = soup.find("tr",{"class":"weatherhistory_results_datavalue temp_mx"})
    for i in maxtemp:
        maxtemp2 = i.find("span",{"class": "value"})

    mintemp3 = mintemp2.text
    maxtemp3 = maxtemp2.text

    SH.cell(row=NXrow, column=1).value = "ZIP"
    SH.cell(row=NXrow, column=2).value = datey2
    SH.cell(row=NXrow, column=3).value = URL
    SH.cell(row=NXrow, column=4).value = mintemp3
    SH.cell(row=NXrow, column=5).value = maxtemp3
    WB.save('File.xlsx')

这样,当'datey'小于或等于'endDate'时,while循环的内容将继续循环,直到'datey'大于'endDate'

导入语句也位于文件的顶部,并且不属于循环。