?抱着《python学习手册》啃了很久,心里想着要动手写点东西,但是一直拖延症到最近才真正开始准备。一开始不知道写点啥好,就从生活中挖掘,发现自己每天查天气预报查的挺频繁的,那就爬一波天气预报吧。
class Weather(object):
def __init__(self):
self.day = time.strftime('%d', time.localtime(time.time()))
self.month = time.strftime('%m', time.localtime(time.time()))
?在类的开始定义了一个构造函数,里面有两个属性:day和month分别是今天的日期和当月月份,这里就用到time模块中的time、localtime、strftime。详细的介绍可以看菜鸟教程,我们现场实践下。
?这里先导入time模块,然后time.time()是当前时间的一个时间戳,需要用localtime()格式化成time.struct_time类型的对象。(struct_time是在time模块中定义的表示时间的对象)然后再用strftime()接收struct_time对象,返回一个字符串表示的时间值,如图所示,我码字的时候正是5月23号。
def get_weather_html(self):
#打开中国天气网的40天天气预报的网页,爬取数据
url = 'http://www.weather.com.cn/weather40d/101020100.shtml'
driver = webdriver.Chrome()
driver.get(url)
driver.maximize_window()
driver.implicitly_wait(3)
month_input = raw_input('please input month:')
if month_input != '' and month_input != self.month:
self.select_month(month_input,driver)
?如果想用selenium的话,要先下个驱动下载地址我用的是谷歌浏览器,所以对应的驱动是cromedriver.exe,注意驱动跟版本强相关,没有下对版本的话就会报错。驱动放在python.exe同目录下即可。上面代码主要做的事情就是:
?这里要强调下implicitly_wait作为隐式等待可以在页面元素找到的时间提前退出等待,但是这里设置的全局的等待时候,假设代码里的路径有错或者其他什么问题,会导致调试时间变长。也可以使用time.sleep(3)这种显式等待,设3s就是等待3s,不管元素找到与否。selenium-python里有很多selenium的详细介绍,关于显示等待和隐式等待的也有。
def select_month(self, month, driver):
driver.find_element_by_xpath('//*[@class="zong"]').click()
month_path = '//div[@class="w_nian"]/ul[2]/li['+month+']'
driver.find_element_by_xpath(month_path).click()
for week_index in range(2,8):
for day_index in range(1,8):
week = str(week_index)
day = str(day_index)
day_path = '//*[@id="table"]/tbody/tr['+week+']/td['+day+']/h2/span[2]'
max_temp_path = '//*[@id="table"]/tbody/tr['+week+']/td['+day+']/div[1]/p/span[1]'
min_temp_path = '//*[@id="table"]/tbody/tr['+week+']/td['+day+']/div[1]/p/span[2]'
date = driver.find_element_by_xpath(day_path)
date = date.text
try:
date_int = int(date)
except(ValueError):
break
if month_input == '':
month = self.month
else:
month = month_input
if date_int == 01 and week_index != 2:
print u'19年{}月每日温度情况如下'.format(month)
break
max_temp = driver.find_element_by_xpath(max_temp_path)
min_temp = driver.find_element_by_xpath(min_temp_path)
max_temp = re.findall('\d+', max_temp.text)
min_temp = re.findall('\d+', min_temp.text)
if __name__ == '__main__':
weather = Weather()
dict,month = weather.get_weather_html()
count = 0
for key in dict.keys():
monthRange = calendar.monthrange(2019, int(month))
print u"{name:10d}号:{max}~{min}℃".format(name=key,max=dict[key][0],min=dict[key][1])
count += 1
if count == monthRange[-1]:
break
?最后我们就可以创建类实例,运行方法,打印数据了。提醒下cmd下运行的话,中文字符前要加个u转成Unicode,不然会报错。下图就是最后的运行结果啦。
?其实获取html的方法有很多种页面获取方法,我一开始用了urllib和request都失败了,因为天气预报的页面是动态加载的,而这两个库只能帮助我们获取静态页面,然后我就想试试自己模拟请求,实现动态加载,无奈才疏学浅,最后只能用万能牌selenium。selenium会模拟用户操作,真的在你电脑上打开浏览器然后点点点,这样虽然万能,但是就很慢呐。 以上就是我代码的大部分了,源码的话可以去我的github获取。
原文:https://www.cnblogs.com/cts2710/p/10920377.html