1. #read1.html文件
    # <html><head><title>The Dormouse's story</title></head>
    # <body>
    # <p class="title"><b>The Dormouse's story</b></p>
    #
    # <p class="story">Once upon a time there were three little sisters; and their names were
    # <a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
    # <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
    # <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
    # and they lived at the bottom of a well.</p>
    #
    # <p class="story">...</p></body></html>
  2.  
  3. #!/usr/bin/env python
    # # -*- coding:UTF-8 -*-
  4.  
  5. import os
    import re
    import requests
    from bs4 import NavigableString
    from bs4 import BeautifulSoup
  6.  
  7. curpath=os.path.dirname(os.path.realpath(__file__))
    hmtlpath=os.path.join(curpath,'read1.html')
  8.  
  9. res=requests.get(hmtlpath)
  10.  
  11. soup=BeautifulSoup(res.content,features="html.parser")
  12.  
  13. for str in soup.stripped_strings:
    print(repr(str))
  14.  
  15. links=soup.find_all(class_="sister")
    for parent in links.parents:
    if parent is None:
    print(parent)
    else:
    print(parent.name)
  16.  
  17. print(links.next_sibling)
  18.  
  19. for link in links:
    print(link.next_element)
    print(link.next_sibling)
  20.  
  21. print(link.privous_element)
    print(link.privous_sibling)
  22.  
  23. def has_class_no_id(tag):
    return tag.has_attr('class') and not tag.has_attr('id')
  24.  
  25. def not_lacie(href):
    return href and not re.compile("lacie").search(href)
  26.  
  27. def not_tillie(href):
    return href and not re.compile("tillie").search(href)
  28.  
  29. def not_tillie1(id):
    return id and not re.compile("link2").search(id)
  30.  
  31. file=open("soup.html","r",encoding="utf-8")
    soup=BeautifulSoup(file,features="lxml")
  32.  
  33. #find_all用法
    tags=soup.find_all(re.compile('^b'))
    tags=soup.find_all('b')
    tags=soup.find_all(['a','b'])
    tags=soup.find_all(has_class_no_id)
    tags=soup.find_all(True)
    tags=soup.find_all(href=not_lacie)
    for tag in tags:
    print(tag.name)
  34.  
  35. def surrounded_by_strings(tag):
    return (isinstance(tag.next_element, NavigableString)
    and isinstance(tag.previous_element, NavigableString))
  36.  
  37. tags=soup.find_all(id=not_tillie1)
    for tag in tags:
    print(tag)
  38.  
  39. tags=soup.find_all(attrs={"id":"link3"})
    for tag in tags:
    print(tag)
  40.  
  41. soup.find_all(recursive=False)
    tags=soup.select("body a")
    tags=soup.select("p > a")
    tags=soup.select("p > #link1")
    tags=soup.select("html head title")
    tags=soup.select(".sister")
    tags=soup.select("[class~=sister]")
    tags=soup.select("#link1 + .sister")
    tags=soup.select("#link1")
    tags=soup.select("a#link1")
    tags=soup.select("a[href]")
    tags=soup.select('a[href^="http://example"]')
    tags=soup.select('a[href$="tillie"]')
    tags=soup.select('a[href*=".com/el"]')
    for tag in tags:
    print(tag)
  1. file=open("soup.html","r",encoding="utf-8")
    soup=BeautifulSoup(file,features="html.parser")
    soup=BeautifulSoup(file,features="html.parser")
    print(soup.prettify())
    print(type(soup))
    print(type(soup.title))
    print(type(soup.title.string))
    print(type(soup.b.string))
  2.  
  3. print(soup.head.name)
    print(soup.title.name)
    print(soup.a.name)
    print(soup.name)
  4.  
  5. tag=soup.a
    print(tag["href"])
    print(tag.string)
    print(tag["class"])
    print(tag.attrs)
  6.  
  7. print(soup.title.string)
    print(soup.title.name)
    print(soup.p.attrs)
    print(soup.a.attrs)
    print(soup.a["class"])

python3爬虫03(find_all用法等)的更多相关文章

  1. python3爬虫(find_all用法等)

    #read1.html文件 # <html><head><title>The Dormouse's story</title></head> ...

  2. python3爬虫系列19之反爬随机 User-Agent 和 ip代理池的使用

    站长资讯平台:python3爬虫系列19之随机User-Agent 和ip代理池的使用我们前面几篇讲了爬虫增速多进程,进程池的用法之类的,爬虫速度加快呢,也会带来一些坏事. 1. 前言比如随着我们爬虫 ...

  3. Python3爬虫系列:理论+实验+爬取妹子图实战

    Github: https://github.com/wangy8961/python3-concurrency-pics-02 ,欢迎star 爬虫系列: (1) 理论 Python3爬虫系列01 ...

  4. 笔趣看小说Python3爬虫抓取

    笔趣看小说Python3爬虫抓取 获取HTML信息 解析HTML信息 整合代码 获取HTML信息 # -*- coding:UTF-8 -*- import requests if __name__ ...

  5. python3爬虫中文乱码之请求头‘Accept-Encoding’:br 的问题

    当用python3做爬虫的时候,一些网站为了防爬虫会设置一些检查机制,这时我们就需要添加请求头,伪装成浏览器正常访问. header的内容在浏览器的开发者工具中便可看到,将这些信息添加到我们的爬虫代码 ...

  6. Python3 爬虫之 Scrapy 核心功能实现(二)

    博客地址:http://www.moonxy.com 基于 Python 3.6.2 的 Scrapy 爬虫框架使用,Scrapy 的搭建过程请参照本人的另一篇博客:Python3 爬虫之 Scrap ...

  7. Python3 爬虫之 Scrapy 框架安装配置(一)

    博客地址:http://www.moonxy.com 基于 Python 3.6.2 的 Scrapy 爬虫框架使用,Scrapy 的爬虫实现过程请参照本人的另一篇博客:Python3 爬虫之 Scr ...

  8. python3爬虫--反爬虫应对机制

    python3爬虫--反爬虫应对机制 内容来源于: Python3网络爬虫开发实战: 网络爬虫教程(python2): 前言: 反爬虫更多是一种攻防战,针对网站的反爬虫处理来采取对应的应对机制,一般需 ...

  9. python3爬虫(4)各种网站视频下载方法

    python3爬虫(4)各种网站视频下载方法原创H-KING 最后发布于2019-01-09 11:06:23 阅读数 13608 收藏展开理论上来讲只要是网上(浏览器)能看到图片,音频,视频,都能够 ...

随机推荐

  1. Linux操作系统的内存使用方法详细解析

    我是一名程序员,那么我在这里以一个程序员的角度来讲解Linux内存的使用. 一提到内存管理,我们头脑中闪出的两个概念,就是虚拟内存,与物理内存.这两个概念主要来自于linux内核的支持. Linux在 ...

  2. 【转】Subversion快速入门教程-动画演示

    如何快速建立Subversion服务器,并且在项目中使用起来,这是大家最关心的问题,与CVS相比,Subversion有更多的选择,也更加的容易,几个命令就可以建立一套服务器环境,可以使用起来,这里配 ...

  3. Codeforces Round #523 (Div. 2)C(DP,数学)

    #include<bits/stdc++.h>using namespace std;long long a[100007];long long dp[1000007];const int ...

  4. Oracle12c 数据库找到Scott账户的方法

    因为Oracle12c数据库中引入了CDB与PDB的概念(具体介绍请参考潇湘隐者的文章http://www.cnblogs.com/kerrycode/p/3386917.html),我们之前常用的练 ...

  5. 洛谷P3649 [APIO2014]回文串(回文自动机)

    传送门 话说回文自动机我自己都还没搞懂呢…… 等到时候会了再来填坑 //minamoto #include<cstdio> #include<cstring> #define ...

  6. Fiddler 基础教程详解

    Fiddler是最强大最好用的Web调试工具之一,它能记录所有客户端和服务器的http和https请求,允许你监视,设置断点,甚至修改输入输出数据. 使用Fiddler无论对开发还是测试来说,都有很大 ...

  7. 多线程中wait、notify理解

    实在惭愧,java开发多年,多线程运用一直不多,该知识点理解也不够,不怎么会用.赶上使用多线程 生产者.消费者模式,学习下该知识点. synchronized  获取锁 wait 阻塞本线程,释放对象 ...

  8. 分层图最短路【bzoj2834】: 回家的路

    分层图最短路[bzoj2834]: 回家的路 题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2834 这道题难在建边. 自己写的时候想到了 ...

  9. 安装php过程中的错误和解决方式 configure: error: jpeglib.h not found

    centos6.5 32位系统: checking for the location of libpng... yeschecking for the location of libXpm... no ...

  10. Python网络爬虫(一)

    Urllib发送请求 基本用法 基本的用法就是调用request库, class urllib.request.Request(url, data=None, headers={}, origin_r ...