Python 为非简单代码段创建while循环
我是Python新手(一般来说也是编程新手),创建while循环有困难。我能找到的所有例子都非常简单,沿着var=10减去1直到得到0。我有一段更大的代码,只要满足一个条件,我就需要重新运行,但我所尝试的没有成功。我正在Anaconda中使用Python 3 我已经尝试了def main()和下面的代码,现在我正在尝试while true(在我的代码示例中,我有while true和while down,它是缩进的,但我似乎无法使它在这里缩进)。我试图让它做的是检查datey是否小于endDate,如果小于,则重新开始整个过程 以下是我所拥有的:Python 为非简单代码段创建while循环,python,web-scraping,while-loop,Python,Web Scraping,While Loop,我是Python新手(一般来说也是编程新手),创建while循环有困难。我能找到的所有例子都非常简单,沿着var=10减去1直到得到0。我有一段更大的代码,只要满足一个条件,我就需要重新运行,但我所尝试的没有成功。我正在Anaconda中使用Python 3 我已经尝试了def main()和下面的代码,现在我正在尝试while true(在我的代码示例中,我有while true和while down,它是缩进的,但我似乎无法使它在这里缩进)。我试图让它做的是检查datey是否小于endDat
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')
从openpyxl导入加载\u工作簿
尽管如此:
WB=load_工作簿('File.xlsx',data_only=True)
SH=WB['02474']
导入日期时间
从日期时间导入时间增量
endDate=datetime.datetime.strtime('2018-02-09','%Y-%m-%d')
MXrow=SH.max\u行
NXrow=MXrow+1
datex=SH.cell(行=MXrow,列=2)
datex2=datex.value
datey=datetime.datetime.strptime(datex2,%Y-%m-%d')+datetime.timedelta(天=1)
datey2=datey.strftime(“%Y-%m-%d”)
URL='1〕https://www.example.com“+str(日期2)
导入请求
page=请求.get(URL)
从bs4导入BeautifulSoup
soup=BeautifulSoup(page.text,'lxml')
mintemp=soup.find(“tr”,“class”:“weatherhistory\u results\u datavalue temp\u mn”})
对于mintemp中的i:
mintemp2=i.find(“span”,“class”:“value”})
maxtemp=soup.find(“tr”,“class”:“weatherhistory\u results\u datavalue temp\u mx”})
对于maxtemp中的i:
maxtemp2=i.find(“span”,{“class”:“value”})
mintemp3=mintemp2.text
maxtemp3=maxtemp2.text
while(datey如果要检查
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您需要在while循环中以某种方式更新datey
,否则您将得到一个无限循环。datey应该在每次通过循环时都进行更改。但是,我不需要在while循环开始之前定义datey和endDate吗?或者它是否在while循环中查找这些定义?这就是我不断获得con的地方融合。很抱歉,问题不是很清楚。你需要重新运行的“较大的代码”是什么?整个代码?如果你想“重新开始整个过程”,那么你必须把“整个过程”放在在while
循环中,不仅仅是最后几行。啊,是的,这是我想要重新运行的全部代码。我尝试在开始时使用def main()将全部代码放入while循环,但它似乎无法识别。