使用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 ...
随机推荐
- SHA1WithRSA
SHA1WithRSA:用SHA算法进行签名,用RSA算法进行加密. 算法说明: 在对进行SHA1算法进行签名后,要求对签名后的数据进行处理,而不是直接进行RSA算法进行加密. 要求把SHA1签名 ...
- BZOJ1972:[SDOI2010]猪国杀(模拟)
Description 太长就不贴过来了 Solution 这个题是真的不难写……唯一的难度就在于理解题意上面……感觉这就是个阅读理解题啊…… 而且你三国杀玩的越多可能就越难写因为你无法理解那些猪的思 ...
- BZOJ3573:[HNOI2014]米特运输(树形DP)
Description 米特是D星球上一种非常神秘的物质,蕴含着巨大的能量.在以米特为主要能源的D星上,这种米特能源的运输和储 存一直是一个大问题.D星上有N个城市,我们将其顺序编号为1到N,1号城市 ...
- 【webpack】config/index.js
// see http://vuejs-templates.github.io/webpack for documentation. var path = require('path') module ...
- 初始化列表initializer_list
初始化列表定义在<initializer_list>,初始化列表简化了参数数量可变的函数的编写,初始化列表的所有的元素都应该是同一种数据类型 由于定义了列表中允许的类型,所以初始化列表是安 ...
- Struts2通配符
action: struts: or: 请求路径:
- [TJOI]2013 最长上升子序列
这个题据说是Splay,或者说是平衡树的模板题,但是我还是不会做--唉-- \(\color{red}{Description}\) 给定一个序列,初始为空.现在我们将\(1\)到\(N\)的数字插入 ...
- C#中HttpWebRequest的用法详解(转载)
1.HttpWebRequest和HttpWebResponse类是用于发送和接收HTTP数据的最好选择.2.命名空间:System.Net3.HttpWebRequest对象不是利用new关键字创建 ...
- 用javascript编写地区表单ES6
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- LeetCode41.缺失的第一个正数 JavaScript
给定一个未排序的整数数组,找出其中没有出现的最小的正整数. 示例 1: 输入: [1,2,0] 输出: 3 示例 2: 输入: [3,4,-1,1] 输出: 2 示例 3: 输入: [7,8,9,11 ...