zoukankan      html  css  js  c++  java
  • 阅读OReilly.Web.Scraping.with.Python.2015.6笔记---BeautifulSoup---findAll

    阅读OReilly.Web.Scraping.with.Python.2015.6笔记---BeautifulSoup---findAll

     

     

    1..BeautifulSoup库的使用

    BeautifulSoup通常用来分析爬虫抓取的Web文档。

    其中findAll函数的使用情景:

    链接:http://www.pythonscraping.com/pages/warandpeace.html 中内容如下:

     

    文字部分有黑色,红色,和绿色的,其决定因素主要在于其中的:

     

    “<span class=”red”>

    “<span class=”green”>

    实现功能:提取出这篇文章中的所有绿色文字。

    代码如下:

    # -*- coding: utf-8 -*-
    """
    Spyder Editor
    
    This is a temporary script file.
    """
    
    from urllib.request import urlopen
    from bs4 import BeautifulSoup
    html = urlopen("http://www.pythonscraping.com/pages/warandpeace.html")
    bsObj = BeautifulSoup(html,"lxml")
    nameList = bsObj.findAll("span",{"class":"green"})
    for name in nameList:
        print(name.get_text())

    代码运行结果:

    Anna
    Pavlovna Scherer
    Empress Marya
    Fedorovna
    Prince Vasili Kuragin
    Anna Pavlovna
    St. Petersburg
    the prince
    Anna Pavlovna
    Anna Pavlovna
    the prince
    the prince
    the prince
    Prince Vasili
    Anna Pavlovna
    Anna Pavlovna
    the prince
    Wintzingerode
    King of Prussia
    le Vicomte de Mortemart
    Montmorencys
    Rohans
    Abbe Morio
    the Emperor
    the prince
    Prince Vasili
    Dowager Empress Marya Fedorovna
    the baron
    Anna Pavlovna
    the Empress
    the Empress
    Anna Pavlovna's
    Her Majesty
    Baron
    Funke
    The prince
    Anna
    Pavlovna
    the Empress
    The prince
    Anatole
    the prince
    The prince
    Anna
    Pavlovna
    Anna Pavlovna

    结果分析:提取出了文中所有绿色文字的内容。

    关于bsObj.findAll(tagName,tagAttributes)的调用

    .findAll()最常用的参数为:tagName,tagAttributes

    tagName指的是"h1","h2","h3"之类的标签

    tagAttributes是一个字典类型的数据,指的是{"class":"green","class":"red"}之类的数据。

  • 相关阅读:
    数据增强
    变态跳台阶
    跳台阶
    数据分析--简单回测框架开发
    数据分析--羊驼交易法则(选股)
    数据分析--动量策略vs反转策略(选股)
    数据分析--PEG策略(选股)
    数据分析--布林带策略(择时)
    数据分析--均值回归策略(选股)
    数据分析--单因子选股策略、多因子选股策略(选股)
  • 原文地址:https://www.cnblogs.com/chensimin1990/p/6600971.html
Copyright © 2011-2022 走看看