#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> #!/usr/bin/env python
# # -*- coding:UTF-8 -*- import os
import re
import requests
from bs4 import NavigableString
from bs4 import BeautifulSoup curpath=os.path.dirname(os.path.realpath(__file__))
hmtlpath=os.path.join(curpath,'read1.html') res=requests.get(hmtlpath) soup=BeautifulSoup(res.content,features="html.parser") for str in soup.stripped_strings:
print(repr(str)) links=soup.find_all(class_="sister")
for parent in links.parents:
if parent is None:
print(parent)
else:
print(parent.name) print(links.next_sibling) for link in links:
print(link.next_element)
print(link.next_sibling) print(link.privous_element)
print(link.privous_sibling) def has_class_no_id(tag):
return tag.has_attr('class') and not tag.has_attr('id') def not_lacie(href):
return href and not re.compile("lacie").search(href) def not_tillie(href):
return href and not re.compile("tillie").search(href) def not_tillie1(id):
return id and not re.compile("link2").search(id) file=open("soup.html","r",encoding="utf-8")
soup=BeautifulSoup(file,features="lxml") #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) def surrounded_by_strings(tag):
return (isinstance(tag.next_element, NavigableString)
and isinstance(tag.previous_element, NavigableString)) tags=soup.find_all(id=not_tillie1)
for tag in tags:
print(tag) tags=soup.find_all(attrs={"id":"link3"})
for tag in tags:
print(tag) 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)
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)) print(soup.head.name)
print(soup.title.name)
print(soup.a.name)
print(soup.name) tag=soup.a
print(tag["href"])
print(tag.string)
print(tag["class"])
print(tag.attrs) 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. HTML5与CSS3实例教程(第2版) 附源码 中文pdf扫描版

    HTML5和CSS3技术是目前整个网页的基础.<HTML5与CSS3实例教程(第2版)>共分3部分,集中讨论了HTML5和CSS3规范及其技术的使用方法.这一版全面讲解了最新的HTML5和 ...

  2. Lxc的cgroup技术

    你将学到什么 什么是cgroup 如何使用cgroup Cgroup简介 CGroup是Control Groups的缩写,是Linux内核提供的一种可以限制.记录.隔离进程组所使用的硬件资源的机制. ...

  3. Unity3D -- shader光照常用函数和变量

    上一篇记录了shader常用函数和变量,这篇记录一些光照计算时常用函数和变量 1.内置的光照变量 _LightColor0 float4 //该Pass处理的逐像素光源的颜色 _WorldSpaceL ...

  4. 51nod1118(递推)

    题目链接: https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1118 题意: 中文题诶~ 思路: 因为机器人只能往下或者右 ...

  5. 洛谷P3043 [USACO12JAN]牛联盟Bovine Alliance

    P3043 [USACO12JAN]牛联盟Bovine Alliance 题目描述 Bessie and her bovine pals from nearby farms have finally ...

  6. Luogu P5103 「JOI 2016 Final」断层 树状数组or线段树+脑子

    太神仙了这题... 原来的地面上升,可以倒着操作(时光倒流),转化为地面沉降,最后的答案就是每个点的深度. 下面的1,2操作均定义为向下沉降(与原题意的变换相反): 首先这个题目只会操作前缀和后缀,并 ...

  7. Django基础(1)

    昨日内容回顾: 1. socket创建服务器 2. http协议: 请求协议 请求首行 请求方式 url?a=1&b=2 协议 请求头 key:value 请求体 a=1&b=2(只有 ...

  8. 简述raid0,raid1,raid5,raid10 的工作原理及特点

    RAID 0 支持1块盘到多块盘,容量是所有盘之和 RAID1 只支持2块盘,容量损失一块盘 RAID 5最少三块盘,不管硬盘数量多少,只损失一块容量 RAID 10最少4块盘,必须偶数硬盘,不管硬盘 ...

  9. GUI的最终选择 Tkinter(三):Checkbutton组件和Radiobutton组件、LabelFrame组件

    Checkbutton组件 Checkbutton组件就是常见的多选按钮,而Radiobutton则是单选按钮 from tkinter import * root = Tk() v = IntVar ...

  10. 09.Spring Bean 注册 - BeanDefinitionRegistry

    基本概念 BeanDefinitionRegistry ,该类的作用主要是向注册表中注册 BeanDefinition 实例,完成 注册的过程. 它的接口定义如下: public interface ...