zoukankan      html  css  js  c++  java
  • 2018-7-12python爬取历史天气数据

    2018-7-12python爬取历史天气数据

    需求

    需要几个城市的历史天气数据,为了方便最后入库,需要的字段为 城市、温度、天气。最好能生成一个完整的csv导入数据。

    from bs4 import BeautifulSoup as bsp
    import urllib,http.cookiejar,re,time
    #对excel的操作,可以添加sheet
    import xlwt
    #拼接两个csv
    import glob
    import time
    
    # 做好cookie管理工作
    cookie=http.cookiejar.CookieJar() # 创建空CookieJar
    cj=urllib.request.HTTPCookieProcessor(cookie) # 构造cookie
    opener = urllib.request.build_opener(cj) # 根据cookie构造opener
    # 伪造header
    opener.addheaders = [('User-agent', 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:37.0) Gecko/20100101 Firefox/37.0'),
                         ('Connection',' keep-alive')]
    # 载入header
    urllib.request.install_opener(opener)
    
    # 定义若干url
    root_url='http://www.tianqihoubao.com/lishi/'
    
    # 获取root_url网页内容
    req = urllib.request.Request(root_url)
    u=bsp(bytes.decode(urllib.request.urlopen(req).read(),'gbk'))
    
    # 找到表格
    u1=u.find(class_="citychk")
    
    # 找到表格里的每一个市
    u2=u1.find_all('dd')
    
    
    # 记录每一个市的url和对应的中文名称
    city_urls=[]
    city_names=[]
    
    #for uu in u2:
    #    u3=uu.find_all('a')
    #    for u3u in u3:
    #        city_urls.append(re.split('.|/',u3u.attrs['href'])[2])
    #        city_names.append(u3u.text)
    
    
    
    ####  shg 2018/07/12 ###############
    ####  需要哪些城市将那些城市的拼音和汉字放到以下两个列表中,月份同理
    city_urls=["nanjing","beijing"]
    city_names=["南京","北京"]
    months=['201712','201801','201802','201803','201804','201805','201806','201807']
    ####  shg 2018/07/12 ###############
    
    
    
    # 构造各市历史月份url
    for i,city_url in enumerate(city_urls):
        # 每爬一个市休息2秒钟
        time.sleep(2)
        with open('D:/天气数据/'+str(i)+city_names[i]+'.csv','w') as f:
            #####  只给第一个csv建列名
            if i == 0 :
                f.write('日期,天气状况,气温,城市,风力风向
    ')
            for month in months:
                url_month='http://www.tianqihoubao.com/lishi/'+city_url+'/month/'+month+'.html'
                req = urllib.request.Request(url_month)
    
                #如果连接不成功,休息5分钟
                while True:
                    try:
                        u=bsp(bytes.decode(urllib.request.urlopen(req).read(),'gbk'))
                        break
                    except:
                        time.sleep(300)
    
                # 删去不可见字符
                u1=[re.sub('s','',x.text) for x in u.table.find_all('td')]
    
                # 写入文件的时候跳过表头
                for j,item in enumerate(u1[4:]):
                    if j%4==3:
                        # 城市名称列####  shg 2018/07/12 #################
                        f.write(city_names[i]+",")
                        f.write(item+'
    ')
                    else:
                        f.write(item+',')
                        
                        
                        
    ####  shg 2018/07/12 #################
    #import glob   #import time
    ##### 拼接多个城市的csv
    #####  注意修改路径
    csvx_list = glob.glob('D:/天气数据/*.csv')
    print('总共发现%s个CSV文件'% len(csvx_list))
    time.sleep(2)
    for i in csvx_list:
        fr = open(i,'r').read()
        with open('weather_history.csv','a') as f:
            f.write(fr)
    print('拼接完毕!')
    ####  shg 2018/07/12 #################
    
    
    
    

    在做的过程中遇到了几个问题,记录如下:

    1 写入excel中,分成多个sheet

    导入xlwt包。

    import xlwt
    # 新建一个xls文件,注意编码
    workbook = xlwt.Workbook(encoding='utf-8')
    #  新建一个sheet,如果对一个单元格重复操作,添加cell_overwrite_ok=True
    booksheet1 = workbook.add_sheet("beijing",cell_overwrite_ok=True)
    #  写入
    booksheet1.write(1,1,"beijing")
    booksheet2 = workbook.add_sheet("jing",cell_overwrite_ok=True)
    booksheet2.write(1,1,"jing")
    # 保存文件
    workbook.save('weather.xls')
    

    2 拼接多个csv文件。

    纵向的拼接,需要两个csv文件的列相同,注意两点:

    1. 如果是循环跑出来的表,每个表都有表头,拼接的时候不会去掉。
    2. 拼接的时候是按照文件名默认排序从上到下依次排列的。
    import glob
    import time
    
    csvx_list = glob.glob('D:/天气数据/*.csv')
    print('总共发现%s个CSV文件'% len(csvx_list))
    time.sleep(2)
    print('正在处理............')
    for i in csvx_list:
        fr = open(i,'r').read()
        with open('csv_to_csv.csv','a') as f:
            f.write(fr)
        print('写入成功!')
    print('写入完毕!')
    print('10秒钟自动关闭程序!')
    
    

    3 获取网页上表格的方法

    [re.sub('s','',x.text) for x in u.table.find_all('td')]
    

    re.sub("替换的内容“,”替换成“,需替换的字符串)

    • 替换的内容可以使用正则表达式, 为转义字符;| 为或,可以拼接多个条件;例如:
    import re
    re.sub('[|]|"|"','','["88585465","64325165","1685654"]')
    
    >>> '88585465,64325165,1685654'
    
  • 相关阅读:
    Python 爬虫 解决escape问题
    python 爬虫 重复下载 二次请求
    iOS开发-消息通知机制(NSNotification和NSNotificationCenter)
    iOS开发-UITableView自定义Cell
    iOS开发-自定义UIAlterView(iOS 7)
    iOS开发-CocoaPods实战
    iOS开发-UICollectionView实现瀑布流
    iOS开发-UITabBarController详解
    iOS 开发-Certificate、App ID和Provisioning Profile之间的关系
    iOS开发-View中frame和bounds区别
  • 原文地址:https://www.cnblogs.com/shgwater/p/9302222.html
Copyright © 2011-2022 走看看