python--爬虫百度天气数据
2018-03-07 15:39
155 查看
#coding=utf-8
'''
从百度或者http://www.weather.com.cn获取天气情况
'''
from selenium import webdriver
import time
class BaiduWeather():
def __init__(self):
self.query_url = 'http://www.baidu.com/s?wd=%stianqi '
self.location = 'shenzhen'
self.dr = webdriver.Firefox()
self.dr.get(self.total_url)
@property
def today_css_selectors(self):
today_main_klass = '.op_weather4_twoicon_today.%s'
return {
'temp':today_main_klass %(' op_weather4_twoicon_temp'),
'weather':today_main_klass %(' op_weather4_twoicon_weath'),
'wind':today_main_klass%(' op_weather4_twoicon_wind')
}
@property
def total_url(self):
return self.query_url %(self.location)
def get_weather(self,date='today'):
if date == 'today':
self.get_today_weather()
elif date == 'tomorrow':
self.get_tomorrow_weather_v1()
else:
raise RuntimeError('Error:unkown date')
def get_today_weather(self):
return self.get_weatehr_by_css(self.today_css_selectors)
@property
def tomorrow_weather_js_hash(self):
format = 'return $("%s").first().text().trim()'
return {
'temp':format %(self.tomorrow_css_selectors['temp']),
'weather':format %(self.tomorrow_css_selectors['weather']),
'wind':format %(self.tomorrow_css_selectors['wind'])
}
@property
def get_weather_by_css(self,css_hash):
temp = self.dr.find_element_by_css_selector(css_hash['temp']).text
weather = self.dr.find_element_by_css_selector(css_hash['weather']).text
wind = self.dr.find_element_by_css_selector(css_hash['wind']).text
return {
'temp': temp,
'weather': weather,
'wind': wind
}
def get_tomorrow_weather_v1(self):
return {
'temp':self.dr.execute_script(self.tomorrow_weather_js_hash['temp']),
'weather':self.dr.execute_script(self.tomorrow_weather_js_hash['weather']),
'wind':self.dr.execute_script(self.tomorrow_weather_js_hash['wind'])
}
def get_tomorrow_weather_v2(self):
return self.get_weather_by_css(self.tomorrow_css_selectors)
def quit(self):
self.quit()
'''
从百度或者http://www.weather.com.cn获取天气情况
'''
from selenium import webdriver
import time
class BaiduWeather():
def __init__(self):
self.query_url = 'http://www.baidu.com/s?wd=%stianqi '
self.location = 'shenzhen'
self.dr = webdriver.Firefox()
self.dr.get(self.total_url)
@property
def today_css_selectors(self):
today_main_klass = '.op_weather4_twoicon_today.%s'
return {
'temp':today_main_klass %(' op_weather4_twoicon_temp'),
'weather':today_main_klass %(' op_weather4_twoicon_weath'),
'wind':today_main_klass%(' op_weather4_twoicon_wind')
}
@property
def total_url(self):
return self.query_url %(self.location)
def get_weather(self,date='today'):
if date == 'today':
self.get_today_weather()
elif date == 'tomorrow':
self.get_tomorrow_weather_v1()
else:
raise RuntimeError('Error:unkown date')
def get_today_weather(self):
return self.get_weatehr_by_css(self.today_css_selectors)
@property
def tomorrow_weather_js_hash(self):
format = 'return $("%s").first().text().trim()'
return {
'temp':format %(self.tomorrow_css_selectors['temp']),
'weather':format %(self.tomorrow_css_selectors['weather']),
'wind':format %(self.tomorrow_css_selectors['wind'])
}
@property
def get_weather_by_css(self,css_hash):
temp = self.dr.find_element_by_css_selector(css_hash['temp']).text
weather = self.dr.find_element_by_css_selector(css_hash['weather']).text
wind = self.dr.find_element_by_css_selector(css_hash['wind']).text
return {
'temp': temp,
'weather': weather,
'wind': wind
}
def get_tomorrow_weather_v1(self):
return {
'temp':self.dr.execute_script(self.tomorrow_weather_js_hash['temp']),
'weather':self.dr.execute_script(self.tomorrow_weather_js_hash['weather']),
'wind':self.dr.execute_script(self.tomorrow_weather_js_hash['wind'])
}
def get_tomorrow_weather_v2(self):
return self.get_weather_by_css(self.tomorrow_css_selectors)
def quit(self):
self.quit()
相关文章推荐
- 基于百度API接口的python数据爬虫解析1【笔记】
- python--爬虫天气数据
- Python爬虫扒取2345天气预报网上的邯郸天气数据
- 【Python】Python简单网络爬虫-爬取近几年的天气数据
- 基于scrapy爬虫的天气数据采集(python)
- 利用Python爬虫爬取天气数据
- Python爬虫:抓取手机APP的数据
- Python爬虫---提取数据(2)--beautifulsoup
- Python爬虫学习,记一次抓包获取js,从js函数中取数据的过程
- Python爬虫之抓取数据——正则表达式/Beautiful Soup/Lxml
- Python爬虫(urllib2+bs4)+分析找出谁是水贴王(1)--数据收集
- 开启菜鸟的python爬虫与数据分析之旅
- Python网络数据采集13:用爬虫测试网站
- 用python+selenium从百度获取本地明日的天气信息并根据温度情况设置提醒
- Python的网页爬虫&文本处理&科学计&机器学习&数据挖掘工具集
- 《网络爬虫-Python和数据分析》数据库建库建表问题
- Python3爬虫小程序——爬取各类天气信息
- [Python爬虫] 之二十:Selenium +phantomjs 利用 pyquery通过搜狗搜索引擎数据
- Python爬虫之HDU提交数据
- 【Python数据分析】简单爬虫 爬取知乎神回复