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"}之类的数据。

  • 相关阅读:
    Node.js安装及环境配置之Windows篇
    C++ STL中Map的按Key排序和按Value排序
    设计模式之观察者模式(c++)
    C/C++回调函数
    c++ string详解 assign
    C++ unsigned long 转化为 unsigned char*
    unsigned char 与unsigned long互换
    微信小程序的登陆流程详解
    谷歌帮:中国最牛的创业帮派
    创业公司打造顶级团队的七个方法
  • 原文地址:https://www.cnblogs.com/chensimin1990/p/6600971.html
Copyright © 2011-2022 走看看