• 中国天气网七天天气


    from urllib import request,parse
    
    import re
    from bs4 import BeautifulSoup
    
    url = "http://www.weather.com.cn/weather/101280601.shtml"
    headers = {
        'User-Agent' : r'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36',
        'Referer':'http://www.weather.com.cn/weather/101280601.shtml',
        'Connection':'keep-alive'
    }
    
    
    try:
        req = request.Request(url, headers=headers)
        resp = request.urlopen(req)
        res = resp.read()
        soup = BeautifulSoup(res, 'lxml')
        uls = soup.find('ul', class_='t clearfix')
        weather = []
        if uls is not None:
            lis = uls.find_all('li')
            i = 0
            for li in lis:
                day = li.h1.string.replace(r'
    ','')
                wea = li.find('p',class_='wea').string.replace('
    ','')
                tem = li.find('p',class_='tem').get_text().replace('
    ','')
                win = li.find('p',class_='win').get_text().replace('
    ','')
                weather.append({'day':day,'wea':wea,'tem':tem,'win':win})
            print(weather)
        else:
            print("未请求到数据")
    except Exception as e:
        print(e)
    

      

    不知道什么原因,总是一次请求到,一次请求不到

  • 相关阅读:
    Centos6.8部署jumpserver(完整版)
    Linux系统下inode满了导致无法写文件的解决思路
    MySQL5.7.20报错Access denied for user 'root'@'localhost' (using password: NO)
    深浅拷贝
    集合
    列表 元组 range
    机器码和字节码
    函数
    文件操作
    小数据池和代码块
  • 原文地址:https://www.cnblogs.com/brady-wang/p/8391645.html
Copyright © 2020-2023  润新知