zoukankan      html  css  js  c++  java
  • BeautifulSoup使用注意事项

    BeautifulSoup使用注意事项

            BeautifulSoup是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式.Beautiful Soup会帮你节省数小时甚至数天的工作时间. 

            一个爬取中国天气网数据的简单示例如下:

      (http://www.weather.com.cn/weather/101010100.shtml网页中的数据使用BeautifulSoup解析)

    import requests
    from bs4 import BeautifulSoup
    
    resp = requests.get('http://www.weather.com.cn/weather/101010100.shtml')
    resp.encoding = 'utf-8'
    beautifulsoup = BeautifulSoup(resp.text, 'html.parser')
        此时,beautifulsoup数据类型为bs4.BeautifulSoup
    available_weather = beautifulsoup.find("div", {'id': '7d'}).find('ul').find_all('li')
      此时,available_weather数据类型为bs4.element.ResultSet
    for item in available_weather:
        date = item.find('h1').string
      此时,date数据类型为 bs4.element.NavigableString

    总之,获得的数据类型不是string

    需要作为字符串使用时,可使用以下方式转换:
    date = ''.join(date)
  • 相关阅读:
    课程个人总结
    构建之法阅读笔记06
    构建之法读后感5
    第五周进度条
    提高自身能力
    活动图与状态机图
    对分析业务模型----类图的学习与认识
    需求分析工作的基本道理
    问题账户需求分析
    2016秋季个人阅读计划
  • 原文地址:https://www.cnblogs.com/zacharyVic/p/8899136.html
Copyright © 2011-2022 走看看