使用Python的BeautifulSoup 类库采集网页内容
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 类库采集网页内容的更多相关文章
- python利用beautifulSoup写爬虫
python BeautifulSoup模块的安装 安装包下载地址:http://www.crummy.com/software/BeautifulSoup/#Download 文档:http://w ...
- python——5行代码采集3000+上市公司信息
毕业季也到了找工作的季节了,很多小伙伴都会一家一家的公司去看,这得多浪费时间啊.今天用Python教大家怎么采集公司的信息,相信大家会很喜欢这个教程的,nice! 基本环境配置 版本:Python3 ...
- python安装第三方类库的方法
1.先到官网 http://pypi.python.org/pypi/setuptools 下载setuptools.exe文件并安装 点击 ez_setup.py进入, 并将内容复制下来, 保存为本 ...
- 搭建基于python +opencv+Beautifulsoup+Neurolab机器学习平台
搭建基于python +opencv+Beautifulsoup+Neurolab机器学习平台 By 子敬叔叔 最近在学习麦好的<机器学习实践指南案例应用解析第二版>,在安装学习环境的时候 ...
- Python配合BeautifulSoup读取网络图片并保存在本地
本例为Python配合BeautifulSoup读取网络图片,并保存在本地. BeautifulSoup可代替正则表达式,更好地解析Html文本,获取其中的指定内容,如Tag.Property等 # ...
- phpQuery轻松采集网页内容
原文地址:phpQuery轻松采集网页内容作者:陌上花开 phpQuery是一个基于PHP的服务端开源项目,它可以让PHP开发人员轻松处理DOM文档内容,比如获取某新闻网站的头条信息.更有意思的是,它 ...
- python 网络请求类库 requests 使用
python 网络请求类库 requests 使用 requests是 为python封装的强大 REST 操作类库 githubhttps://github.com/kennethreitz/req ...
- python scrapy,beautifulsoup,regex,sgmparser,request,connection
In [2]: import requests In [3]: s = requests.Session() In [4]: s.headers 如果你是爬虫相关的业务?抓取的网站还各种各样, ...
- Ubuntu下一个python的BeautifulSoup和rsa安装方法---信息检索project2部分:微博爬行要求python包裹
后果<信息检索>第二project,微博具有抓取数据,再处理.师兄给了代码.让慢慢爬.可是在ubuntu下.少了非常多python软件包.须要安装. 1.首先执行时.说少了python.B ...
随机推荐
- kubernetes 入门学习
kubernetes 学习 kubernetes 简介 Kubernetes这个名字源自希腊语,意思是"舵手",也是"管理者","治理者"等 ...
- TensorFlow函数(二)tf.get_variable() 和 tf.Variable()
tf.Variable(<initial - value>,name=<optional - name>) 此函数用于定义图变量.生成一个初始值为initial - value ...
- D. Frets On Fire 【二分,前缀和】 (Codeforces Global Round 2)
题目传送门:http://codeforces.com/contest/1119/problem/D D. Frets On Fire time limit per test 1.5 seconds ...
- Android UI【android 仿微信、QQ聊天,带表情,可翻页,带翻页拖动缓冲】
http://blog.csdn.net/lnb333666/article/details/8546497 如题,这是公司项目的一个功能模块,先上个效果图: 其次大致说说原理: 1,首先判断输入的字 ...
- nRF5 SDK for Mesh(三) Installing the mesh toolchain 安装编译工具链
Installing the mesh toolchain To build the example applications, a toolchain based on either CMake o ...
- Gradle Goodness: Add Incremental Build Support to Custom Tasks with Annotations
In a previous post we learned how we can use the inputs and outputs properties to set properties or ...
- 【星云测试】开发者测试(2)-采用精准测试工具对J2EE Guns开发框架进行测试
配置测试Guns Guns简介 Guns是一个近几年来基于SpringBoot的开源便利且较新的JavaEE项目开发框架,它整合了springmvc + shiro + mybatis-plus + ...
- 轻量ORM-SqlRepoEx (十三)最佳实践
ORM-SqlRepoEx 是 .Net平台下兼容.NET Standard 2.0,一个实现以Lambda表达式转转换标准SQL语句,使用强类型操作数据的轻量级ORM工具,在减少魔法字串同时,通过灵 ...
- 【腾讯敏捷转型No.3】Scrum有什么好?
在敏捷转型的过程中,除了敏捷宣言中的四个价值观和十二条原则以外,并没有太多比较权威的理论实践. 如图一,敏捷宣言中的四个价值观: (图一) 四条敏捷核心价值观指出了敏捷的核心思想,但是并没有仔细说明具 ...
- bootstrap世界探索1——山川河流(文字排版)
世界到底是什么?其实世界很简单,正所谓一花一世界,一树一菩提,世界就在我们身边.造物神是伟大的,在我看来无论是HTML,css,js都可以看作是一个世界,但是他们是构成宏观世界不可或缺的,正如IU框架 ...