zoukankan      html  css  js  c++  java
  • Python网络爬虫——bs4基本用法

    使用流程:

      -导包:from bs4 import BeautifulSoup

      - 使用方式:可以将一个html文档,转化为BeautifulSoup对象,然后通过对象的方法或属性去查找指定的节点内容。

        (1)转化本地文件:

          - soup = BeautifulSoup(open(‘本地文件’),‘lxml’)

        (2)转化网络文件(与requests配合):

          - page_text = requests.get(url=url,headers=headers).text

          - soup = BeautifulSoup(page_text,'lxml')

        (3)打印soup对象显示内容为html文件中的内容

    基础语法:

       (1)根据标签名查找

        - soup.a  只能找到第一个符合要求的标签

      (2)获取属性

        - soup.a.atters  获取a所有的属性和属性值,返回一个字典

        - soup.a.atters['href']  获取href属性

        - soup.a['href']  上面的简写

      (3)***获取内容

        - soup.a.string  获取<a></a>之间的文本内容(a不含子标签)

          - soup.a['href'].string  获取a标签中href的内容

        - soup.a.text   获取<a></a>之间包括子标签的所有文本内容

        - soup.a.get_text()  同上面用法

        【注意】如果标签里还有标签,那么string获取到的结果为None,其他两个可以获取文本内容

      (4)***find:找到第一个符合要求的标签

        - soup.find('a')  找到第一个符合要求的标签(a标签)

        - soup.find('a',title='xxx')   找到第一个title=‘xxx’的a标签

        - soup.find('a',alt='xxx')  同上

        - soup.find('a',class_='xxx')  同上,【注意】class后的_

        - soup.find('a',id='xxx')  同上

      (5)***find_all:找到所有符合要求的标签

        - soup.find_all('a')  找到所有a标签

        - soup.find(['a','b'])  找到所有a和b标签

        - soup.find_all('a',limit=2)  限制前两个

      (6)***根据选择器选择指定的内容

          select:soup.select('.feng')  选择class属性值为feng的所有标签

        - 常见的选择器:标签选择器(a)、类型选择器(.)、id选择器(#)、层级选择器

          - 层级选择器:

            div > p > a > .lala  只能选择最下面一级 class=lala的子标签

            div .dudu  div下面clas=dudu的所有字标签

        【注意】 select选择器返回永远是列表,需要通过下表提取指定的对象

    实例:使用bs4实现将诗词名句网站中三国演义小说的每一章的内同爬取到本地磁盘进行存储

    import requests
    from bs4 import BeautifulSoup
    
    #获得url url
    = 'http://www.shicimingju.com/book/sanguoyanyi.html' headers = { 'User-Agent':'' }

    #获取网页并转换成BeautifulSoup对象 page_text
    = requests.get(url=url,headers=headers).text soup = BeautifulSoup(page_text,'lxml')

    #选取class=book-mulu的div标签 >下的ul标签 >下的li标签 >下的所有a标签 a_list
    = soup.select('.book-mulu>ul>li>a')

    #创建sanguo.txt文件 fp
    = open('sanguo.txt','w',encoding='utf-8')

    #遍历所有a标签
    for a in a_list:
      #获取a标签文本 title
    = a.string
      #获取a标签中href属性的文本信息,并组成url detail_url
    = 'http://www.shicimingju.com' + a['href']
      #获取新url内的页面信息 detail_page_text
    = requests.get(url=detail_url,headers=headers).text #将新url网页创建为BeautifulSoup对象 title_soup = BeautifulSoup(detail_page_text,'lxml')
      #获取新url内class='chapter_content'的div标签的文本信息 content
    = title_soup.find('div',class_='chapter_content').text
      #将标题与正文间加入 并写入sanguo.txt文件 fp.write(title
    +' '+content)
      #每一章节下载完毕都打印成功
    print(title,'下载完毕') print('over') fp.close()
  • 相关阅读:
    关于lockkeyword
    关于多层for循环迭代的效率优化问题
    Android 面试精华题目总结
    Linux基础回想(1)——Linux系统概述
    linux源代码编译安装OpenCV
    校赛热身 Problem C. Sometimes Naive (状压dp)
    校赛热身 Problem C. Sometimes Naive (状压dp)
    校赛热身 Problem B. Matrix Fast Power
    校赛热身 Problem B. Matrix Fast Power
    集合的划分(递推)
  • 原文地址:https://www.cnblogs.com/bilx/p/11542252.html
Copyright © 2011-2022 走看看