Python beautifulsoup模块
BeautifulSoup中文文档:https://www.crummy.com/software/BeautifulSoup/bs4/doc.zh/
BeautifulSoup下载:http://www.crummy.com/software/BeautifulSoup/
解压到任意目录
在cmd控制台下进入目录
执行:python setup.py install即可;
执行完后命令行进入python使用import bs4命令验证是否成功:
假设content变量里存着整个网页的字符串,或者是urllib.request.urlopen(url)的返回值
首先,导入模块,然后把content打包进soup里
from bs4 import BeautifulSoup
soup = BeautifulSoup(content,'html.parser')
1.将字符串以网页的形式美化显示(返回的是一个字符串)
print(soup.prettify())
2.提取出网页中的特定标签
比如:提取出所有<a>标签
soup = BeautifulSoup(content,'html.parser')
print(soup.find_all('a'))
或者提取出所有<a>标签和<b>标签
soup = BeautifulSoup(content,'html.parser')
print(soup.find_all(['a','b']))
或者提取出所有class为t-large的<span>标签(也就是所有类似于<span class="t-large"></span>的标签)
soup = BeautifulSoup(content,'html.parser')
print(soup.find_all('span','t-large'))
或者提取出所有有class属性没有id属性的标签
def has_class_but_no_id(tag):
return tag.has_attr('class') and not tag.has_attr('id')
soup = BeautifulSoup(content,'html.parser')
print(soup.find_all(has_class_but_no_id))
或者提取出所有id等于"link2"的标签
soup = BeautifulSoup(content,'html.parser')
print(soup.find_all(id="link2"))
3.获取一个标签(一个soup对象)的内容.contents
print(soup.contents)
print(soup.a.contents)
4.获取一个标签的class属性(要特别注意返回的是list,哪怕只有一个元素,因为HTML新特性——多属性导致的)
print(soup.a['class'])
5.删除一个标签
>>> soup = BeautifulSoup('<script>a</script>baba<script>b</script>')
>>> [s.extract() for s in soup('script')]
>>> soup
baba
6.删除一个特定class的标签
from bs4 import BeautifulSoup markup = '<a>This is not div <div class="1">This is div 1</div><div class="2">This is div 2</div></a>'
soup = BeautifulSoup(markup,"html.parser")
a_tag = soup soup.find('div',class_='').decompose() print a_tag #<a>This is not div <div class="1">This is div 1</div></a>
7.注意在beautifulsoup中,<br>标签写成<br/>
8.提取一个soup里的所有字符串
for string in soup.strings:
print(repr(string))
提取一个soup里的非空行非空白字符串
for string in soup.stripped_strings:
print(repr(string))
Python beautifulsoup模块的更多相关文章
- python BeautifulSoup模块的简要介绍
常用介绍: pip install beautifulsoup4 # 安装模块 from bs4 import BeautifulSoup # 导入模块 soup = BeautifulSoup(ht ...
- Python 爬虫三 beautifulsoup模块
beautifulsoup模块 BeautifulSoup模块 BeautifulSoup是一个模块,该模块用于接收一个HTML或XML字符串,然后将其进行格式化,之后遍可以使用他提供的方法进行快速查 ...
- python中BeautifulSoup模块
BeautifulSoup模块是干嘛的? 答:通过html标签去快速匹配标签中的内容.效率相对比正则会好的多.效率跟xpath模块应该差不多. 一:解析器: BeautifulSoup(html,&q ...
- 孤荷凌寒自学python第六十八天学习并实践beautifulsoup模块1
孤荷凌寒自学python第六十八天学习并实践beautifulsoup模块1 (完整学习过程屏幕记录视频地址在文末) 感觉用requests获取到网页的html源代码后,更重要的工作其实是分析得到的内 ...
- Python爬虫之Beautifulsoup模块的使用
一 Beautifulsoup模块介绍 Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式.Be ...
- python学习之BeautifulSoup模块爬图
BeautifulSoup模块爬图学习HTML文本解析标签定位网上教程多是爬mzitu,此网站反爬限制多了.随意找了个网址,解析速度有些慢.脚本流程:首页获取总页数-->拼接每页URL--> ...
- python 常用模块(转载)
转载地址:http://codeweblog.com/python-%e5%b8%b8%e7%94%a8%e6%a8%a1%e5%9d%97/ adodb:我们领导推荐的数据库连接组件bsddb3:B ...
- Python - BeautifulSoup 安装
BeautifulSoup 3.x 1. 下载 BeautifulSoup. [huey@huey-K42JE python]$ wget http://www.crummy.com/software ...
- 【爬虫入门手记03】爬虫解析利器beautifulSoup模块的基本应用
[爬虫入门手记03]爬虫解析利器beautifulSoup模块的基本应用 1.引言 网络爬虫最终的目的就是过滤选取网络信息,因此最重要的就是解析器了,其性能的优劣直接决定这网络爬虫的速度和效率.Bea ...
随机推荐
- 图表框架HelloCharts(3)饼状图
1 效果图 2 xml文件 activity_pie_chart.xml <FrameLayout xmlns:android="http://schemas.android.com/ ...
- UVA 820 Internet Bandwidth 因特网宽带(无向图,最大流,常规)
题意:给一个无向图,每条边上都有容量的限制,要求求出给定起点和终点的最大流. 思路:每条无向边就得拆成2条,每条还得有反向边,所以共4条.源点汇点已经给出,所以不用建了.直接在图上跑最大流就可以了. ...
- Java [Leetcode 268]Missing Number
题目描述: Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is ...
- SQLlite(WebSQL)如何排序并分页查询(SQLlite语法)
SELECT * FROM Table ORDER BY ID DESC Limit 10,9 limit语义:跳过10行,取9行 参考: SQLite的limit用法 如果我要去11-20的Ac ...
- 修改Oracle 表空间名称 tablespace name
修改表空间名称步骤如下: 1. 使用oracle用户登录执行 $sqlplus / as sysdba 2. 执行修改表空间命令如下 SQL> alter tablespace TEST re ...
- Activity与Activity之间,Fragment与Fragment之间通过Bundle传值的研究
一.Fragment与Activity的通讯 在使用fragment的时候,通常的用法都是使用一个activity来管理不同的fragment,所以每个fragment与activity的及时通讯 ...
- wireshark tcp 协议分析 z
虽然知道wireshark是抓包神器,只会大概大概用一下,还用一下下tcpdump,略懂一点BPF过滤器,也知道一点怎么用 wirkshark过滤相关的报文,但是对于详细的字段的含义,如何查看TCP的 ...
- 1、ListView自定义控件下拉刷新(一)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layo ...
- Matlab编程实例(3) 函数向左或向右平移N点 左移右移
%函数移动 close all; clear all; dir=input('请输入平移方向,“1”为向左,“2”为向右'); if dir~=1&&dir~=2;%输入控制 e ...
- bzoj 2186 [Sdoi2008]沙拉公主的困惑(欧拉函数,逆元)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2186 [题意] 若干个询问,求1..n!中与m!互质的个数. [思路] 首先有gcd( ...