使用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 ...
随机推荐
- 学习python第二天数据库day1
day01: 关键字:desc 作用:查看表结构(字段名,数据类型&长度) 举例: desc python1808_laoguo; 追加数据到表中:(新增操作) 关键字:insert into ...
- sublime text 3 搭建python ide
SublimeCodeIntel 代码提示插件 安装步骤: 1.打开Package Control[Preferences>>Package Control] 2.输入 install 选 ...
- 一般处理程序中,获取session
注意了: 1.要在一般处理程序中获取其他页面的session值,需要引用名空间: using System.Web.SessionState; 2.然后继承一个接口:IRequiresSessionS ...
- [转]Ribbon界面介绍(1)
小弟最近在学习VS2010中Ribbon界面的介绍,相比与C#的界面设计的强大,C++的界面实在太难做了,但没办法,项目需求,又不得不做,遍查网络上的资料,发现有用的基本上就是MSDN的帮助,又是全英 ...
- 【转】上传jar包到nexus私服
原文:https://my.oschina.net/lujianing/blog/297128 1通过网页上传 这种方法只是上传了jar包.通过maven引用当前jar,不能取得jar的依赖 from ...
- (转)查看mysql数据库连接数、并发数相关信息
查看mysql数据库连接数.并发数相关信息 1.mysql> show status like 'Threads%';+-------------------+-------+| Variabl ...
- HttpWebRequest类,及如何使用Cookie(转载)
HttpWebRequest类与HttpRequest类的区别.HttpRequest类的对象用于服务器端,获取客户端传来的请求的信息,包括HTTP报文传送过来的所有信息.而HttpWebReques ...
- 内核调试工具——strace
简介 strace常用来跟踪进程执行时的系统调用和所接收的信号. 在Linux世界,进程不能直接访问硬件设备,当进程需要访问硬件设备(比如读取磁盘文件,接收网络数据等等)时,必须由用户态模式切换至内核 ...
- CCF认证201803-1 跳一跳
import java.util.Scanner; public class Jump { public static void main(String[] args) { Scanner sc = ...
- 杂记(那些我还容易混淆的c和c++知识)
1: 定义一个对象时先调用基类的构造函数.然后调用派生类的构造函数:析构的时候恰好相反:先调用派生类的析构函数.然后调用基类的析构函数.2: 多态性具体体现在运行和编译两个方面:在程序运行时的多态性 ...