Scrapy实战篇(五)之爬取历史天气数据

  本篇文章我们以抓取历史天气数据为例,简单说明数据抓取的两种方式:

  1、一般简单或者较小量的数据需求,我们以requests(selenum)+beautiful的方式抓取数据

  2、当我们需要的数据量较多时,建议采用scrapy框架进行数据采集,scrapy框架采用异步方式发起请求,数据抓取效率极高。

  下面我们以http://www.tianqihoubao.com/lishi/网站数据抓取为例进行进行两种数据抓取得介绍:  

  1、以request+bs的方式采集天气数据,并以mysql存储数据

  思路:

  我们要采集的天气数据都在地址 http://www.tianqihoubao.com/lishi/beijing/month/.html 中存储,观察url可以发现,url中只有两部分在变化,即城市名称和你年月,而且每年都固定包含12个月份,可以使用 months = list(range(1, 13))构造月份,将城市名称和年份作为变量即可构造出需要采集数据的url列表,遍历列表,请求url,解析response,即可获取数据。

  

  以上是我们采集天气数据的思路,首先我们需要构造url链接。

  

 def get_url(cityname,start_year,end_year):
years = list(range(start_year, end_year))
months = list(range(1, 13))
suburl = 'http://www.tianqihoubao.com/lishi/'
urllist = []
for year in years:
for month in months:
if month < 10:
url = suburl + cityname + '/month/'+ str(year) + (str(0) + str(month)) + '.html'
else:
url = suburl + cityname + '/month/' + str(year) + str(month) + '.html'
urllist.append(url.strip())
return urllist

通过以上函数,可以得到需要抓取的url列表。  

  可以看到,我们在上面使用了cityname,而cityname就是我们需要抓取的城市的城市名称,需要我们手工构造,假设我们已经构造了城市名称的列表,存储在mysql数据库中,我们需要查询数据库获取城市名称,遍历它,将城市名称和开始年份,结束年份,给上面的函数。

 def get_cityid(db_conn,db_cur,url):
suburl = url.split('/')
sql = 'select cityid from city where cityname = %s '
db_cur.execute(sql,suburl[4])
cityid = db_cur.fetchone()
idlist = list(cityid)
return idlist[0]

  有了城市代码,开始和结束年份,生成了url列表,接下来当然就是请求地址,解析返回的html代码了,此处我们以bs解析网页源代码,代码如下:

 def parse_html_bs(db_conn,db_cur,url):
proxy = get_proxy()
proxies = {
'http': 'http://' + proxy,
'https': 'https://' + proxy,
}
headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36',
'Connection': 'close'
} # 获取天气数据的html网页源代码
weather_data = requests.get(url=url, headers=headers,proxies = proxies).text
weather_data_new =(weather_data.replace('\n','').replace('\r','').replace(' ',''))
soup = BeautifulSoup(weather_data_new,'lxml')
table = soup.find_all(['td'])
# 获取城市id
cityid = get_cityid(db_conn, db_cur, url)
listall = []
for t in list(table):
ts = t.string
listall.append(ts)
n= 4
sublist = [listall[i:i+n] for i in range(0,len(listall),n)]
sublist.remove(sublist[0])
flist = []
# 将列表元素中的最高和最低气温拆分,方便后续数据分析,并插入城市代码
for sub in sublist:
if sub == sublist[0]:
pass
sub2 = sub[2].split('/')
sub.remove(sub[2])
sub.insert(2, sub2[0])
sub.insert(3, sub2[1])
sub.insert(0, cityid) # 插入城市代码
flist.append(sub)
return flist

  最后我们在主函数中遍历上面的列表,并将解析出来的结果存储到mysql数据库。

 if __name__ == '__main__':
citylist = get_cityname(db_conn,db_cur)
for city in citylist:
urllist = get_url(city,2016,2019)
for url in urllist:
time.sleep(1)
flist = parse_html_bs(db_conn, db_cur, url)
for li in flist:
tool.dyn_insert_sql('weather',tuple(li),db_conn,db_cur)
time.sleep(1)

以上我们便完成了以requests+bs方式抓取历史天气数据,并以mysql存储的程序代码,完成代码见:https://gitee.com/liangxinbin/Scrpay/blob/master/weatherData.py

  2、用scrapy框架采集天气数据,并以mongo存储数据

  1)定义我们需要抓取的数据结构,修改框架中的items.py文件

 class WeatherItem(scrapy.Item):
# define the fields for your item here like:
# name = scrapy.Field()
cityname = Field() #城市名称
data = Field()    #日期
tq = Field()     #天气
maxtemp=Field()   #最高温度
mintemp=Field()   #最低温度
fengli=Field()    #风力

  2)修改下载器中间件,随机获取user-agent,ip地址

 class RandomUserAgentMiddleware():
def __init__(self,UA):
self.user_agents = UA @classmethod
def from_crawler(cls, crawler):
return cls(UA = crawler.settings.get('MY_USER_AGENT')) #MY_USER_AGENT在settings文件中配置,通过类方法获取 def process_request(self,request,spider):
request.headers['User-Agent'] = random.choice(self.user_agents) #随机获取USER_AGENT def process_response(self,request, response, spider):
return response class ProxyMiddleware():
def __init__(self):
ipproxy = requests.get('http://localhost:5000/random/') #此地址为从代理池中随机获取可用代理
self.random_ip = 'http://' + ipproxy.text def process_request(self,request,spider):
print(self.random_ip)
request.meta['proxy'] = self.random_ip def process_response(self,request, response, spider):
return response

  3)修改pipeline文件,处理返回的item,处理蜘蛛文件返回的item

  

 import pymongo

 class MongoPipeline(object):

     def __init__(self,mongo_url,mongo_db,collection):
self.mongo_url = mongo_url
self.mongo_db = mongo_db
self.collection = collection @classmethod
def from_crawler(cls,crawler):
return cls(
mongo_url=crawler.settings.get('MONGO_URL'), #MONGO_URL,MONGO_DB,COLLECTION在settings文件中配置,通过类方法获取数据
mongo_db = crawler.settings.get('MONGO_DB'),
collection = crawler.settings.get('COLLECTION')
) def open_spider(self,spider):
self.client = pymongo.MongoClient(self.mongo_url)
self.db = self.client[self.mongo_db] def process_item(self,item, spider):
# name = item.__class__.collection
name = self.collection
self.db[name].insert(dict(item)) #将数据插入到mongodb数据库。
return item def close_spider(self,spider):
self.client.close()

  4)最后也是最重要的,编写蜘蛛文件解析数据,先上代码,在解释

 # -*- coding: utf-8 -*-
import scrapy
from bs4 import BeautifulSoup
from scrapy import Request
from lxml import etree
from scrapymodel.items import WeatherItem class WeatherSpider(scrapy.Spider):
name = 'weather' #蜘蛛的名称,在整个项目中必须唯一
# allowed_domains = ['tianqihoubao']
start_urls = ['http://www.tianqihoubao.com/lishi/'] #起始链接,用这个链接作为开始,爬取数据,它的返回数据默认返回给parse来解析。 #解析http://www.tianqihoubao.com/lishi/网页,提取连接形式http://www.tianqihoubao.com/lishi/beijing.html
def parse(self, response):
soup = BeautifulSoup(response.text, 'lxml')
citylists = soup.find_all(name='div', class_='citychk')
for citys in citylists:
for city in citys.find_all(name='dd'):
url = 'http://www.tianqihoubao.com' + city.a['href']
yield Request(url=url,callback = self.parse_citylist) #返回Request对象,作为新的url由框架进行调度请求,返回的response有回调函数parse_citylist进行解析 #解析http://www.tianqihoubao.com/lishi/beijing.html网页,提取链接形式为http://www.tianqihoubao.com/lishi/tianjin/month/201811.html
def parse_citylist(self,response):
soup = BeautifulSoup(response.text, 'lxml')
monthlist = soup.find_all(name='div', class_='wdetail')
for months in monthlist:
for month in months.find_all(name='li'):
if month.text.endswith("季度:"):
continue
else:
url = month.a['href']
url = 'http://www.tianqihoubao.com' + url
yield Request(url= url,callback = self.parse_weather) #返回Request对象,作为新的url由框架进行调度请求,返回的response由parse_weather进行解析 # 以xpath解析网页数据;
def parse_weather(self,response): #解析网页数据,返回数据给pipeline处理
# 获取城市名称
url = response.url
cityname = url.split('/')[4] weather_html = etree.HTML(response.text)
table = weather_html.xpath('//table//tr//td//text()')
# 获取所有日期相关的数据,存储在列表中
listall = []
for t in table:
if t.strip() == '':
continue
# 替换元素中的空格和\r\n
t1 = t.replace(' ', '')
t2 = t1.replace('\r\n', '')
listall.append(t2.strip())
# 对提取到的列表数据进行拆分,将一个月的天气数据拆分成每天的天气情况,方便数据插入数据库
n = 4
sublist = [listall[i:i + n] for i in range(0, len(listall), n)]
# 删除表头第一行
sublist.remove(sublist[0])
# 将列表元素中的最高和最低气温拆分,方便后续数据分析,并插入城市代码 for sub in sublist:
if sub == sublist[0]:
pass
sub2 = sub[2].split('/')
sub.remove(sub[2])
sub.insert(2, sub2[0])
sub.insert(3, sub2[1])
sub.insert(0, cityname) Weather = WeatherItem() #使用items中定义的数据结构 Weather['cityname'] = sub[0]
Weather['data'] = sub[1]
Weather['tq'] = sub[2]
Weather['maxtemp'] = sub[3]
Weather['mintemp'] = sub[4]
Weather['fengli'] = sub[5]
yield Weather

运行项目,即可获取数据,至此,我们完成了天气数据的抓取项目。

项目完整代码:

https://gitee.com/liangxinbin/Scrpay/tree/master/scrapymodel

Scrapy实战篇(五)之爬取历史天气数据

上一篇:hihocoder 1061.Beautiful String


下一篇:UVA - 10785 The Mad Numerologist