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-tree Shaking
    05-babel-解析高级js语法+polyfill按需注入
    Symbol.iterator
    回调
    finally
    then的参数
    通过简单例子看Promise(一)
    作为Promise构造函数参数的函数
    resolved和rejected
    resolve和reject
  • 原文地址:https://www.cnblogs.com/zacharyVic/p/8899136.html
Copyright © 2011-2022 走看看