zoukankan      html  css  js  c++  java
  • 一天一学,一天一记

    今天解决了程序卡住不动的问题:
    通过设置urllib2.urlopen(url,None,time_out)中time_out的值,来排除连接超时的错误。

    time_out = X
    try:
      data ={'':''} #data为递交表单
      req = urllib2.Request(url,data)
      res = urllib2.urlopen(req,None,time_out)
    except:
      print 'connect again!'
      main()
    

    并且深入学习了 BeautifulSoup这个模块的使用,解决了乱码,学会了筛选多重标签提取元素的方法。

    如果想要查找属性值未知的标签,

      比如这样<tag attr ="XXX">

      可以这样写,用True来代替所有未知或者变化的属性值

      

    soup.find(name = "tag",attrs={"attr":True})
    

    并且,在beautifulsoup里也可以嵌入正则表达式:

      比如遇到这样的标签:

    <div class ="icon">
              <h1 class = "h11ello">hello world 123</h1>
              <h1 class = "h12ello">hello world 456</h1>
              <h1 class = "h13ello">hello world 789</h1>
    </div>
    

      我们就可以:

    list = soup.findAll(name = "h1" ,
            attrs = {"class":re.compile(r"h(d+)ello")})        
    

      这样就可以获得到:

            hello  world 123

            hello  world 456 

            hello  world 789

  • 相关阅读:
    获取网站IP地址(Linux,C)
    linux_c_udp_example
    linux_c_tcp_example
    golang-sort
    docker_jenkins
    依赖抽象,而不要依赖具体实现
    网络杂记
    游戏开发中遇到的问题
    随手杂记
    go多态
  • 原文地址:https://www.cnblogs.com/huangxiaohen/p/3399491.html
Copyright © 2011-2022 走看看