嗨,我是新的python,现在我正在学习如何刮网站。我正在练习循环功能。这里是问题如何在Python中设置两个循环?
i)我正在使用天气站点来获取两个数据:天气和温度。
ii)在该网站的网址中,它包含日期和城市。
麻烦是我只能控制循环的一面,我搜索了网站,没有找到答案。
以下是我写的代码。
# -*- coding: utf-8 -*-
import requests
from bs4 import BeautifulSoup
import time
calday = ["01","02","03","04","05"...."31"]
citylist = ["Newyork","London","Paris"]
for i in range(len(calday)):
for i in range(len(citylist)):
url="http://websitename.com/localeCode=en_US&complexSearchField="+citylist[i]+"&05%2F"+calday+"2F2015" # 05%2F01%2F2015 which means 2015-05-01 and the day is replaced by "+calday+"
wb_data = requests.get(url,headers=headers)
soup = BeautifulSoup(wb_data.text,'lxml')
weathers = soup.select('.searchResultsweather')
temps = soup.select('.searchResultstemp')
time.sleep(2)
for temp,weather in zip(temps,weathers):
data = {
"temp":temp.get_text(),
"weather":weather.get_text()
}
print (data)
我知道有更好的方法来治疗的日期模块,但现在我对如何实现两个回路,并得到类似的结果卡住:
纽约MAY1临时天气
纽约MAY2临时天气
...
纽约may31临时天气
,然后转到伦敦和巴黎的相同格式。
在此先感谢。