BeautifulSoup 一个分析、处理DOM树的类库。可以做网络爬虫。模块简称bs4。

安装类库

easy_install beautifulsoup4  

pip install beautifulsoup4  

下面是一些用法

from urllib.request    import    urlopen
from bs4 import BeautifulSoup
html_doc = """
<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 text-bold text-danger" id="link3" title="this is title!">Tillie</a>;
and they lived at the bottom of a well.</p> <p class="red">...</p>
<p class="green">...</p>
<p class="red green">...</p>
</body>
</html>
""" soup = BeautifulSoup(html_doc, "html.parser") link3 = soup.find(id='link3') # <a class="sister" href="http://example.com/tillie" id="link3" title="this is title!">Tillie</a>
print(link3) # <class 'bs4.element.Tag'>
print(type(link3)) # {'href': 'http://example.com/tillie', 'title': 'this is title!', 'id': 'link3', 'class': ['sister', 'text-bold', 'text-danger']}
print(link3.attrs) # Tillie
print(link3.get_text()) # this is title!
print(link3["title"]) all_a = soup.find_all('a') # <a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>
print(all_a[0]) # ['Elsie', 'Lacie', 'Tillie']
print(soup.find_all(text=["Tillie", "Elsie", "Lacie"])) # [<p class="red green">...</p>]
print(soup.find_all("p", {"class":"red", "class":"red green"}))

一个例子

采集所有img标签的title属性的内容

# -*- coding: utf-8 -*- 

from    urllib.request    import    urlopen
from urllib.error import HTTPError
from bs4 import BeautifulSoup url = "http://qa.beloved999.com/category/view?id=2"
url = "http://beloved.finley.com/category/view?id=24"
html = urlopen(url)
bs = BeautifulSoup(html.read(),"html.parser")
res = bs.findAll("img", "item-image")
print(len(res))
for a in res:
print(a['title'])

注意,有些网站会失败,返回403 forbidden。比如我试的开源中国,可能更header头有关。

经查,发送的HTTP_USER_AGENT是Python-urllib/3.4。包含HTTP的信息有

'HTTP_ACCEPT_ENCODING' => 'identity'
'HTTP_CONNECTION' => 'close'
'HTTP_HOST' => 'beloved.finley.com'
'HTTP_USER_AGENT' => 'Python-urllib/3.4'  。

使用Python的BeautifulSoup 类库采集网页内容的更多相关文章

  1. python利用beautifulSoup写爬虫

    python BeautifulSoup模块的安装 安装包下载地址:http://www.crummy.com/software/BeautifulSoup/#Download 文档:http://w ...

  2. python——5行代码采集3000+上市公司信息

    毕业季也到了找工作的季节了,很多小伙伴都会一家一家的公司去看,这得多浪费时间啊.今天用Python教大家怎么采集公司的信息,相信大家会很喜欢这个教程的,nice! 基本环境配置 版本:Python3 ...

  3. python安装第三方类库的方法

    1.先到官网 http://pypi.python.org/pypi/setuptools 下载setuptools.exe文件并安装 点击 ez_setup.py进入, 并将内容复制下来, 保存为本 ...

  4. 搭建基于python +opencv+Beautifulsoup+Neurolab机器学习平台

    搭建基于python +opencv+Beautifulsoup+Neurolab机器学习平台 By 子敬叔叔 最近在学习麦好的<机器学习实践指南案例应用解析第二版>,在安装学习环境的时候 ...

  5. Python配合BeautifulSoup读取网络图片并保存在本地

    本例为Python配合BeautifulSoup读取网络图片,并保存在本地. BeautifulSoup可代替正则表达式,更好地解析Html文本,获取其中的指定内容,如Tag.Property等 # ...

  6. phpQuery轻松采集网页内容

    原文地址:phpQuery轻松采集网页内容作者:陌上花开 phpQuery是一个基于PHP的服务端开源项目,它可以让PHP开发人员轻松处理DOM文档内容,比如获取某新闻网站的头条信息.更有意思的是,它 ...

  7. python 网络请求类库 requests 使用

    python 网络请求类库 requests 使用 requests是 为python封装的强大 REST 操作类库 githubhttps://github.com/kennethreitz/req ...

  8. python scrapy,beautifulsoup,regex,sgmparser,request,connection

    In [2]: import requests   In [3]: s = requests.Session()   In [4]: s.headers 如果你是爬虫相关的业务?抓取的网站还各种各样, ...

  9. Ubuntu下一个python的BeautifulSoup和rsa安装方法---信息检索project2部分:微博爬行要求python包裹

    后果<信息检索>第二project,微博具有抓取数据,再处理.师兄给了代码.让慢慢爬.可是在ubuntu下.少了非常多python软件包.须要安装. 1.首先执行时.说少了python.B ...

随机推荐

  1. 消息中间件--"rocketmq"01之环境搭建

    前置知识 ssh工具 连接linux工具SecureCRT 颜色设置,参考 中文乱码,参考 Linux相关知识 centos7 防火墙firewalld的基本使用,参考 启动: systemctl s ...

  2. 4.12 Spark环境更新

    addJar方法是做什么的呢?它用于将Jar文件添加到Driver的RPC环境中. 通过addJar和addFile可以将各种任务执行所依赖的文件添加到Driver的RPC环境中, •小结 伴生对象是 ...

  3. deque详解

    deque是double-ended queue的简称,deque和vector几乎上是一样的,使用的非常少,定义在<deque>头文件里: deque和vector的区别在于: 1)de ...

  4. webpack4配置

    一.安装webpack 需要先在项目中npm init初始化一下,生成package.json 建议node版本安装到8.2以上 // webpack4中除了正常安装webpack之外,需要再单独安一 ...

  5. 使用iptables和tc对端口限速

    首先,我们来看一下tc,TC(Traffic Control)命令,是linux自带的告警流控命令.Linux操作系统中的流量控制器TC(Traffic Control)用于Linux内核的流量控制, ...

  6. js判断字符长度 汉字算两个字符

    方法一:使用正则表达式,代码如下: function getByteLen(val) { var len = 0; for (var i = 0; i < val.length; i++) { ...

  7. Spring Cloud Sleuth 之Greenwich版本全攻略

    微服务架构是一个分布式架构,微服务系统按业务划分服务单元,一个微服务系统往往有很多个服务单元.由于服务单元数量众多,业务的复杂性较高,如果出现了错误和异常,很难去定位.主要体现在一个请求可能需要调用很 ...

  8. C/S模式,发布/订阅模式和PUSH/PULL模式(上)

    CS模式(客户端/服务器模式) 最场景的信息传递模式,也称为Request/Response模式,或者调用模式.http/https协议即此模式.因为最常用所以大家一般都比较熟悉,这里不重点讲了,大家 ...

  9. activiti获取到流程图节点信息之任务节点

    //获取bpmnModel对象 BpmnModel model1 = new BpmnJsonConverter().convertToBpmnModel(modelNode); //由于我们这里仅仅 ...

  10. iOS:手势与矩形、点运算相关(18-01-24更)

    1.矩形.点运算 1.获取当前的View在Window的frame 2.包含判断 3.获取点击在响应者 touchesBegan 的位置 4.UIScrollView.UITableView 实时 位 ...