zoukankan      html  css  js  c++  java
  • 中国天气网七天天气

    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)
    

      

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

  • 相关阅读:
    javaEE_maven_struts2_tomcat_first
    企业框架-Spring
    MyBatis延迟加载及缓存
    MyBatis注解及动态Sql
    框架之MyBatis
    SQL中的一些关键字用法
    Mysql————基本sql语句
    表单验证
    java中的锁——列队同步器
    线程同步Lock锁
  • 原文地址:https://www.cnblogs.com/brady-wang/p/8391645.html
Copyright © 2011-2022 走看看