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. 1、Python代码初识

    由于python2.7版本默认使用的是ASCII码,必须在编写前加入两行代码,表示使用UTF-8码. #!/usr/bin/python # -*- coding:utf-8 -*- print('你 ...

  2. 编程算法 - 数字在排序数组中出现的次数 代码(C)

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u012515223/article/details/36869869 数字在排序数组中出现的次数 代 ...

  3. 死磕salt系列-salt grains pillar 配置

    grains 和 pillar 对比: Grains:存放静态数据,主要存储客户端的主机信息,重启grains会刷新. Pillar: 处理敏感数据, 处理差异性的文件. Grains数据系统 sal ...

  4. mysql优化---笔记

    ​一.优化方法:(加粗部分为比较重要的) 1.数据表设计合理:2.索引优化:3.SQL语句优化,定位慢查询explain ;4.分表技术.分区技术:5.读写分离(配置):6.创建适当的存储过程.函数. ...

  5. [Noip2007]Core树网的核

    嘟嘟嘟 首先求树的直径两次bfs即可,实际上bfs就是最短路,因为树上路径是唯一的,所以用任何一种遍历方法都行(spfa和dijkstra当然也可以). 可以证明,只要求出任意一条直径就行了,为什么呢 ...

  6. Spyder中figure显示设置

    Spyder是Python的一个IDE.和其他的Python的IDE相比,它最大的优点就是模仿MATLAB的“工作空间”的功能,可以很方便地观察和修改数组的值. 如果不是进行大规模的工程开发,重点专注 ...

  7. VS2012与VS2015同时安装用VS2012创建MFC程序时弹出编译错误”fatal error C1083: 无法打开包括文件:“mprapidef.h”: No such file or directory”的解决办法

    在WIndows 7操作系统上同时安装VS2012与VS2015并用VS2012创建MFC程序时弹出编译错误”fatal error C1083: 无法打开包括文件:“mprapidef.h”: No ...

  8. Android 配置文件 AndroidManifest 解析

    1.屏幕分辨率 <supports-screens android:smallScreens="true" android:normalScreens="true& ...

  9. nginx编译问题:make[1]: *** [/usr/local/pcre//Makefile] Error 127

    解决方法: 是由于nginx高版本的需要使用pcre原文件路径. 解压pcre-7.9.tar.gz 例如解压后位置在 /home/wang/pcre-7.9位置 使用nginx配置的时候 ./con ...

  10. oracle安装程序异常终止解决办法

    安装Oracle时总是会报程序异常终止,摸不着头脑,作为初学者一下就乱了分寸   工具/原料   Oracle软件包 win764位 方法/步骤     右击Oracle安装图标setup.exe,选 ...