从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 ...
随机推荐
- P1052 过河[DP]
题目描述 在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧.在桥上有一些石子,青蛙很讨厌踩在这些石子上.由于桥的长度和青蛙一次跳过的距离都是正整数,我们可以把独木桥上青蛙可能到达的点看成数 ...
- python_并发编程——管道
1.管道 from multiprocessing import Pipe conn1,conn2 = Pipe() #返回两个值 conn1.send('wdc') #发送 print(conn2. ...
- 浏览器兼容问题--get/post
问题描述: 人员通过发送位置在百度地图上显示出来.删除人员后,chrome地图上该人员也随即消失,但IE浏览器上仍旧存在.清除缓存后,才消失. 原因: IE下面同一个地址,不会多次去请求的.只有加一个 ...
- jquery锚点跳转
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- httpclient: 设置请求的超时时间,连接超时时间等
httpclient: 设置请求的超时时间,连接超时时间等 public static void main(String[] args) throws Exception{ //创建httpclien ...
- aix 10代oracle zabbix2.4.4 日志监控
同一类型的监控项,zabbix 2.4的客户端也支持日志监控,可是在参数个数上有问题,如果把所有参数都放满,监控项会提示too mant parameters,无法 生效取数据, 对于不同的正则式.m ...
- Windows下Apache配置多域名项目
<VirtualHost www.a.com:80> DocumentRoot "C:\object\a\web" ServerName http://www.a.co ...
- (3)Go运算符
运算符 Go 语言内置的运算符有: 算术运算符 关系运算符 逻辑运算符 位运算符 赋值运算符 算数运算符 运算符 描述 + 相加 - 相减 * 相乘 / 相除 % 求余 注意: ++(自增)和--(自 ...
- 如何用elasticsearch构架亿级数据采集系统(第1集:非生产环境windows安装篇)
(一)做啥的? 基于Elasticsearch,可以为实现,大数据量(亿级)的实时统计查询的方案设计,提供底层数据框架. 本小节jacky会在非生产环境下,在 window 系统下,给大家分享着部分的 ...
- NAT反向转换基本配置详解