zoukankan      html  css  js  c++  java
  • python 爬虫系列07-天气爬虫

    看天气

    import requests
    from bs4 import BeautifulSoup
    ALL_DATA = []
    def parse_page(url):
        headers = {
            'User-Agent': "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.84 Safari/537.36"
        }
        response = requests.get(url, headers=headers)
        text = response.content.decode('utf-8')
        soup = BeautifulSoup(text, 'html5lib')
        conMidtab = soup.find('div', class_='conMidtab')
        tables = conMidtab.find_all('table')
        for table in tables:
            trs = table.find_all('tr')[2:]
            for index,tr in enumerate(trs):
                tds = tr.find_all('td')
                city_td = tds[0]
                if index == 0:
                    city_td = tds[1]
                city = list(city_td.stripped_strings)[0]
                temp_td = tds[-2]
                min_temp = list(temp_td.stripped_strings)[0]
                ALL_DATA.append({"城市 ":city,"温度 ":int(min_temp)})
              #  print("城市: %s ,温度%s℃ " % (city,int(min_temp)))
    
    def main():
        urls = {
            'http://www.weather.com.cn/textFC/hb.shtml',
            'http://www.weather.com.cn/textFC/db.shtml',
            'http://www.weather.com.cn/textFC/hd.shtml',
            'http://www.weather.com.cn/textFC/hz.shtml',
            'http://www.weather.com.cn/textFC/hn.shtml',
            'http://www.weather.com.cn/textFC/xb.shtml',
            'http://www.weather.com.cn/textFC/xn.shtml',
            'http://www.weather.com.cn/textFC/gat.shtml'
        }
        for url in urls:
            parse_page(url)
        ALL_DATA.sort(key=lambda data:data['min_temp'])
        print(ALL_DATA)
    if __name__ == "__main__":
        main()
  • 相关阅读:
    Pycharm创建Django项目示例
    Window下MyCat的下载与安装
    Python中使用xlrd、xlwt、xlutils读写Excel文件
    循环队列(Java实现)
    oracle 创建表
    win10 删除文件卡在99%
    python xx005文件操作
    python xx004集合
    python xx003字典
    不理解
  • 原文地址:https://www.cnblogs.com/kingle-study/p/9916199.html
Copyright © 2011-2022 走看看