【发布时间】:2018-05-24 12:20:48
【问题描述】:
我将这个简单的Web Weather Scraping 脚本放在一起,以检查给定位置的温度。该代码运行良好,尽管它可能不是最好的或最干净的版本。还在学习。但它正在抓取:<span _ngcontent-c19="" class="wu-value wu-value-to">67</span> 来自 HERE。
#!/usr/bin/python
# -*- coding: utf-8 -*-
import requests
from BeautifulSoup import BeautifulSoup
import time
degree = u'\N{DEGREE SIGN}'
url = 'https://www.wunderground.com/weather/us/ca/san-diego/KCASANDI355'
response = requests.get(url)
html = response.content
soup = BeautifulSoup(html)
current_temp = soup.find("div", {"class" : "current-temp"}).find("span", {"class" : "wu-value wu-value-to"})
for i in current_temp:
print('San Diego Feels Like ') + i + (degree + 'F')
输出如下所示:
San Diego Feels Like 74°F
我的目标是有一个循环的函数,并根据在 current_temp 变量中定义的当前温度进行打印,例如,如果温度低于 70F It's too cold,或者如果它高于 80F @ 987654328@等
但是,我很难理解如何告诉我的代码执行,或者在这种情况下print 您可以说这些不同的任务或方法?原谅我的英语。 While (True): 循环肯定有问题,但我无法理解它。感谢您的帮助,祝大家周日愉快。
#!/usr/bin/python
import requests
from BeautifulSoup import BeautifulSoup
import time
degree = u'\N{DEGREE SIGN}'
url = 'https://www.wunderground.com/weather/us/ca/san-diego/KCASANDI355'
response = requests.get(url)
html = response.content
soup = BeautifulSoup(html)
current_temp = soup.find("div", {"class" : "current-temp"}).find("span", {"class" : "wu-value wu-value-to"})
def weather():
while(True):
for i in current_temp:
print('San Diego Feels Like ') + i + (degree + 'F')
#time.sleep(2)
if (i <= 70) and (i >= 50):
print('It\'s kinda cool')
break
elif i <= 50:
print('It\'s cold af')
elif (i >= 80) and (i <= 100):
print('It\'s hot af')
break
else:
print('You Dead')
break
if __name__ == "__main__":
weather()
【问题讨论】:
-
我猜
i是一个字符串,但与另一个整数比较时应该是一个整数。
标签: python for-loop web-scraping beautifulsoup