从0开始学爬虫7之BeautifulSoup模块的简单介绍
参考文档:
https://www.crummy.com/software/BeautifulSoup/bs4/doc.zh/
# 安装 beautifulsoup4
(pytools) D:\python\pytools>pip install beautifulsoup4
# coding=utf-8 from bs4 import BeautifulSoup as bs
import re html_doc = """
<html><head><title>The Dormouse's story</title></head> <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>
"""
soup = bs(html_doc, "html.parser") # print(soup.prettify()) # print(soup.title.string)
# print(soup.a)
# print(soup.find(id='link2')) # print(soup.find(id='link2').string) # print(soup.find(id='link2').get_text()) # 获取所有a标签的内容
# for link in soup.find_all('a'):
# print(link.get_text()) # 使用string获取不到p标签的内容
# print(soup.find("p", {"class": "story"}).get_text()) # 查找所有以b开头的标签
# for tag in soup.find_all(re.compile("^b")):
# print(tag.get_text()) # 查找所有的a标签中href以 http://example.com开头的数据,其中的.号代表任意字符,如果需要 表示点号本身可以用\转义 href=re.compile(r"^http://example\.com/")
data = soup.find_all("a", href=re.compile(r"^http://example.com/"))
print(data)
从0开始学爬虫7之BeautifulSoup模块的简单介绍的更多相关文章
- 从0开始学爬虫8使用requests/pymysql和beautifulsoup4爬取维基百科词条链接并存入数据库
从0开始学爬虫8使用requests和beautifulsoup4爬取维基百科词条链接并存入数据库 Python使用requests和beautifulsoup4爬取维基百科词条链接并存入数据库 参考 ...
- 从0开始学爬虫12之使用requests库基本认证
从0开始学爬虫12之使用requests库基本认证 此处我们使用github的token进行简单测试验证 # coding=utf-8 import requests BASE_URL = " ...
- 从0开始学爬虫11之使用requests库下载图片
从0开始学爬虫11之使用requests库下载图片 # coding=utf-8 import requests def download_imgage(): ''' demo: 下载图片 ''' h ...
- 从0开始学爬虫9之requests库的学习之环境搭建
从0开始学爬虫9之requests库的学习之环境搭建 Requests库的环境搭建 环境:python2.7.9版本 参考文档:http://2.python-requests.org/zh_CN/l ...
- 从0开始学爬虫4之requests基础知识
从0开始学爬虫4之requests基础知识 安装requestspip install requests get请求:可以用浏览器直接访问请求可以携带参数,但是又长度限制请求参数直接放在URL后面 P ...
- 从0开始学爬虫3之xpath的介绍和使用
从0开始学爬虫3之xpath的介绍和使用 Xpath:一种HTML和XML的查询语言,它能在XML和HTML的树状结构中寻找节点 安装xpath: pip install lxml HTML 超文本标 ...
- 从0开始学爬虫2之json的介绍和使用
从0开始学爬虫2之json的介绍和使用 Json 一种轻量级的数据交换格式,通用,跨平台 键值对的集合,值的有序列表 类似于python中的dict Json中的键值如果是字符串一定要用双引号 jso ...
- 【爬虫入门手记03】爬虫解析利器beautifulSoup模块的基本应用
[爬虫入门手记03]爬虫解析利器beautifulSoup模块的基本应用 1.引言 网络爬虫最终的目的就是过滤选取网络信息,因此最重要的就是解析器了,其性能的优劣直接决定这网络爬虫的速度和效率.Bea ...
- 【网络爬虫入门03】爬虫解析利器beautifulSoup模块的基本应用
[网络爬虫入门03]爬虫解析利器beautifulSoup模块的基本应用 1.引言 网络爬虫最终的目的就是过滤选取网络信息,因此最重要的就是解析器了,其性能的优劣直接决定这网络爬虫的速度和效率.B ...
随机推荐
- 浅谈OpenStack与虚拟机的区别与联系
很多不太明白OpenStack与虚拟机之间的区别,下面以KVM为例,给大家讲一下他们的区别和联系 OpenStack:开源管理项目OpenStack是一个旨在为公共及私有云的建设与管理提供软件的开源项 ...
- 使用 create-react-app 快速构建 React 开发环境
在终端执行以下命令创建项目: 1.指定创建的项目位置(这里以桌面为例) cd Desktop 2.创建 React 项目 npx create-react-app my-app 3.进入项目并启动 c ...
- brief 程序注释
/** * @brief Acceleration (g's) in body frame. * Embedded MPL defines gravity as positive accelerati ...
- wordpress实现主动推送+熊掌号推送同步进行
今天给一个朋友http://www.myunigift.cn/ 这个站点是用wordpress,今天帮他改造熊掌号,于是做了数据同步推送. 只要把下面的代码写到funtions.php里面,发布文章的 ...
- IDEA使用入门设置(从入门到放弃)
激活 1:设置JDK路径 2:设置maven环境 3:设置tomcat 4:设置快捷键与eclipse相同 4.1:设置字体大小 5:创建maven java 工程并进行编译打包等操作 6:创建 ...
- Oracle 查询结果去重保留一项
首先因为需要查询很多字段,也就排除了使用distinct的可能性. 1.1 原始sql select finalSql.* from (select '' SMS_CONTENT, ' as 短信发出 ...
- HDU - 3555 - Bomb(数位DP)
链接: https://vjudge.net/problem/HDU-3555 题意: The counter-terrorists found a time bomb in the dust. Bu ...
- VS - Paginated
BootstrapPagination.cshtml @model PaginationModel <div class="pagination"> <ul> ...
- 获取上一个页面的data
let pages = getCurrentPages();// 获取页面栈 let current = pages[pages.length - 1]; // 当前页面 let url = curr ...
- loj #6342. 跳一跳 期望dp
令 $f[i]$ 表示已经到达 $i$ 点,为了到大 $n$ 点还期望需要的时间,随便转移一下就行. 由于本题卡空间,要记得开滚动数组. #include <bits/stdc++.h> ...