爬虫的基本概念及BeautifulSoup的基本使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ricardo_leite/article/details/77101178

第一部分 侵入

  1. Cookie登录信息使用、异常处理

    参考:http://www.cnblogs.com/junrong624/p/5533655.html

  2. 网站防盗链

    在HTTP协议中 头信息中有一个很重要的选项 referer
    referer 表示的是网页的来源以及上一页的地址
    如果直接在浏览器输入地址,进入网站,则没有referer头信息

    所以,服务器可根据referer来知道用户从哪个网站进来的和图片是从哪个网站进来的
    如果是通过本站访问的图片是可以正常访问的,否则图片不能正常访问。

    参考:
    http://blog.csdn.net/xiao_tommy/article/details/53186201

  3. 反爬虫

    假如一个网站它会某一段时间某个IP 的访问次数,如果访问次数过多,它会禁止你的访问。所以你可以设置一些来帮助你做工作,每隔一段时间换一个代理。

第二部分 解析(Python的爬虫框架Beautifulsoup的使用)

  1. 对象的种类

    • Tag标签
    • NevigableString节点内容
    • BeautifulSoup文档对象,特殊的tag
    • Comment处理注释内容
  2. 遍历文档树:

    • 直接子节点
      .contents返回数组,可用通过.contents[0]获取元素
      .children返回迭代器,可用通过循环遍历出元素
    • 所有子孙节点
      .descendants
      返回迭代器,循环遍历输出;同时会遍历到节点内容。
    • 节点内容
      .string
    • 多个内容
      .strings
      .stripped_strings
    • 父节点
      .parent
    • 全部父节点
      .parents
    • 兄弟节点
      .next_sibling .previous_sibling
    • 全部兄弟节点
      .next_siblings .previous_siblings
    • 前后节点
      .next_element .previous_element
    • 所有前后节点
      .next_elements .previous_elements
  3. 搜索文档树

    • find_all( name , attrs , recursive , text , **kwargs )
      – name参数,可以传字符串、正则表达式、列表、方法、true
      常用soup. find_all(‘b’)查询b标签
      – Keyword参数,根据标签中的属性筛选;
      常用soup.find_all(“a”, class_=”sister”)
      – text 参数,搜索文档中相匹配的字符串。
      常用soup.find_all(text=”Elsie”)
      – limit 参数,限制记录条数。
      soup.find_all(“a”, limit=2)
      – recursive 参数,find_all会返回所有子孙节点,如果只返回子节点的结果,则
      recursive=False
    • find( name , attrs , recursive , text , **kwargs )
      它与 find_all() 方法唯一的区别是 find_all() 方法的返回结果是值包含一个元素的列表,而 find() 方法直接返回结果
    • find_parents() find_parent()
      find_all() 和 find() 只搜索当前节点的所有子节点,孙子节点等. find_parents() 和 find_parent() 用来搜索当前节点的父辈节点,搜索方法与普通tag的搜索方法相同,搜索文档搜索文档包含的内容
    • find_next_siblings() find_next_sibling()
      这2个方法通过 .next_siblings 属性对当 tag 的所有后面解析的兄弟 tag 节点进行迭代, find_next_siblings() 方法返回所有符合条件的后面的兄弟节点,find_next_sibling() 只返回符合条件的后面的第一个tag节点
    • find_previous_siblings() find_previous_sibling()
      这2个方法通过 .previous_siblings 属性对当前 tag 的前面解析的兄弟 tag 节点进行迭代, find_previous_siblings() 方法返回所有符合条件的前面的兄弟节点, find_previous_sibling() 方法返回第一个符合条件的前面的兄弟节点
    • find_all_next() find_next()
      这2个方法通过 .next_elements 属性对当前 tag 的之后的 tag 和字符串进行迭代, find_all_next() 方法返回所有符合条件的节点, find_next() 方法返回第一个符合条件的节点
    • find_all_previous() 和 find_previous()
      这2个方法通过 .previous_elements 属性对当前节点前面的 和字符串进行迭代, find_all_previous() 方法返回所有符合条件的节点, find_previous()方法返回第一个符合条件的节点
  4. Css选择器

    • 通过标签名查找
      soup.select(‘title’)
    • 通过类名查找
      soup.select(‘.sister’)
    • 通过 id 名查找
      soup.select(‘#link1’)
    • 组合查找(用空格分开)
      soup.select(‘p #link1’)
    • 属性查找
      print soup.select(“head > title”)
      print soup.select(‘a[href=”http://example.com/elsie”]’)

详细信息参考:
http://python.jobbole.com/81349/

猜你喜欢

转载自blog.csdn.net/ricardo_leite/article/details/77101178
今日推荐