安装beautiful soup模块

  Windows:

    pip install beautifulsoup4

  Linux:

    apt-get install python-bs4

  

BS4解析器比较

BS官方推荐使用lxml作为解析器,因为其速度快,也比较稳定。那么lxml解析器是怎么安装的呢?

Windows下安装lxml方法:

  1、pip安装

    pip install lxml

    安装出错,原因是需要Visual c++,在windows下通过pip安装lmxl总会出现问题,如果你非要使用pip去安装的话,就把依赖一一解决了再pip.

  2、手工安装

    1、先在http://www.lfd.uci.edu/~gohlke/pythonlibs/#lxml下载符合自己系统版本的lmxl,如lxml‑3.6.4‑cp27‑cp27m‑win_amd64.whl

    2、安装wheel模块,pip install wheel

    3、安装lxml模块,pip install lxml‑3.6.4‑cp27‑cp27m‑win_amd64.whl

Linux下安装lxml方法:

  apt-get install python-lxml

BS4解析器的使用

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>武汉旅游景点</title>
</head>
<body>
<div id="content">
<div class="title">
<h3>武汉景点</h3>
</div>
<ul class="table">
<li>景点<a>门票价格</a></li>
</ul>
<ul class="content">
<li nu="">东湖<a class="price"></a></li>
<li nu="">磨山<a class="price"></a></li>
<li nu="">欢乐谷<a class="price"></a></li>
<li nu="">海昌极地海洋世界<a class="price"></a></li>
<li nu="" src="http://mm.howkuai.com/wp-content/uploads/2017a/03/06/limg.jpg">玛雅水上乐园<a class="price"></a></li>
</ul>
</div>
</body>
</html>
#!/usr/bin/env python
# _*_ coding:utf- _*_ from bs4 import BeautifulSoup
soup = BeautifulSoup(open("scenery.html"),"lxml")
print soup.prettify()

简单的使用

字符集的问题  

  当一个文件或网页导入BeautifulSoup之后,它会自动地很快猜测出文件或网页的常用字符编码,如果不能自动猜测出来的话可以用exclude_encoding和from_encoding来处理。

    排除某种编码

    soup = BeautifulSoup(open("scenery.html"),exclude_encodings=["iso-8859-7","gb2312"])

    使用某种编码
    soup = BeautifulSoup(open("scenery.html"),from_encoding="big5")

BS解析的原理

  bs4将网页节点解析成了一个个Tag,然后根据标签名称、标签属性名称、标签属性值及顺序等将数据过滤出来。

  1、根据标签名称查找标签

    soup.TagName

    soup.find(TagName)

    soup.find_all(TagName)

#!/usr/bin/env python
# _*_ coding:utf-8 _*_
from bs4 import BeautifulSoup
soup = BeautifulSoup(open("scenery.html"),"lxml")
# 解析出第一个a标签
print soup.a
print soup.find("a")
# 解析出所有a标签
print soup.find_all("a") 结果:
<a>门票价格</a>
<a>门票价格</a>
[<a>\u95e8\u7968\u4ef7\u683c</a>, <a class="price">60</a>, <a class="price">60</a>, <a class="price">108</a>, <a class="price">150</a>, <a class="price">150</a>]

  

 2、标签名称相同时,外加属性值解析数据

  特殊写法:仅适用于查找class的内容,可以理解为专为class而设

    soup.find(TagName,[attrsName])

    soup.find_all(TagName,[attrsName])

  万能写法,还可用于解析自定义属性:

    soup.find(TagName,attrs={AttrName:AttrValue})

    soup.find_all(TagName,attrs={AttrName:AttrValue})

#!/usr/bin/env python
# _*_ coding:utf-8 _*_
from bs4 import BeautifulSoup
soup = BeautifulSoup(open("scenery.html"),"lxml")
# 解析出第一个属性值为price的a标签
print soup.find("a","price")
print soup.find("a",attrs={"class":"price"})
# 解析出所有属性值为price的a标签
print soup.find_all("a","price")  
结果: <a class="price">60</a> <a class="price">60</a> [<a class="price">60</a>, <a class="price">60</a>, <a class="price">108</a>, <a class="price">150</a>, <a class="price">150</a>] <li nu="1">东湖<a class="price">60</a></li>

   

 解析标签值

# 解析属性值
print soup.find("li",attrs={"nu":"1"}).get("nu")
# 解析文本
print soup.find("li",attrs={"nu":"1"}).a.get_text() 结果:
1
60

  

显示属性的值

# 解析出属性nu=1的li标签
nu5 = soup.find("li",attrs={"nu":"5"})
# 解析nu=5的li标签的src属性值
print nu5.attrs['src']

 

根据文本找标签

r = re.compile("texttest")
soup.find("a",text=r).parent 查找内容为texttest的a标签的父标签

  

到此为止可以用BeautifulSoup做些简单的爬虫了。

用BeautifulSoup写一个简单的处理百度贴吧的例子,爬取百度贴吧中权利的游戏的贴子。

 #!/usr/bin/env python
# _*_ coding:utf- _*_
import urllib2
from bs4 import BeautifulSoup
import itemWrite class Item(object):
title = None
firstAuthor = None
firstTime = None
reNum = None
content = None
lastAuthor = None
lastTime = None class GetTiebaInfo(object):
def __init__(self,url):
self.url = url
self.pageSum =
self.urls = self.getUrls(self.pageSum)
self.items = self.spider(self.urls)
self.itemWrite("test.txt",self.items) def getUrls(self,pageSum):
urls = []
pns = [str(i*) for i in range(pageSum)]
ul = self.url.split("=")
for pn in pns:
ul[-] = pn
tmp = "=".join(ul)
urls.append(tmp)
return urls def getResponseContent(self,url):
try:
response = urllib2.urlopen(url.encode("utf8"))
return response.read()
except:
print "url open faild"
return None def spider(self,urls):
items = []
for url in urls:
htmlContent = self.getResponseContent(url)
soup = BeautifulSoup(htmlContent,'lxml')
tagsli = soup.find_all("li",attrs={"class":" j_thread_list clearfix"})
for tag in tagsli:
item = Item()
item.title = tag.find("a",attrs={"class":"j_th_tit "}).get_text().strip()
try:
item.firstAuthor = tag.find("span","frs-author-name-wrap").a.get_text().strip()
except:
item.firstAuthor = 'zzz'
item.firstTime = tag.find("span","pull-right is_show_create_time").get_text().strip()
item.reNum = tag.find("span",attrs={"title":u"回复"}).get_text().strip()
item.content = tag.find("div",attrs={"class":"threadlist_abs threadlist_abs_onlyline "}).get_text().strip()
item.lastAuthor = tag.find("span",attrs={"class":"tb_icon_author_rely j_replyer"}).a.get_text().strip()
item.lastTime = tag.find("span",attrs={"title":u"最后回复时间"}).get_text().strip()
items.append(item)
return items def itemWrite(self,filename,items):
itemWrite.writeTotxt(filename,items) if __name__ == '__main__':
url = u'http://tieba.baidu.com/f?kw=权利的游戏&ie=utf-8&pn=0'
Get = GetTiebaInfo(url)

完整代码

 #!/usr/bin/env python
# _*_ coding:utf- _*_ # 写到文本文件
def writeTotxt(fileName,items):
with open(fileName,'w') as fp:
for item in items:
fp.write("title:%s\t author:%s\t firstTime:%s\n content:%s\n reNum:%s\t lastAuthor:%s\t lastTime:%s\n\n"
%(item.title.encode("utf8"),item.firstAuthor.encode("utf8"),item.firstTime.encode("utf8"),item.content.encode("utf8"),item.reNum.encode("utf8"),item.lastAuthor.encode("utf8"),item.lastTime.encode("utf8"))) # 写到Excel文件 # 写到DB

itemWrite

itemWrite中我只写了一个将数据写入文本的函数,还有写入excel和db的函数没有完善,因为都很简单,不想写了,有个意思就行了。

BeautifulSoup爬虫基础知识的更多相关文章

  1. python 爬虫基础知识一

    网络爬虫(又被称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者),是一种按照一定的规则,自动的抓取万维网信息的程序或者脚本. 网络爬虫必备知识点 1. Python基础知识2. P ...

  2. Python爬虫基础知识入门一

    一.什么是爬虫,爬虫能做什么 爬虫,即网络爬虫,大家可以理解为在网络上爬行的一直蜘蛛,互联网就比作一张大网,而爬虫便是在这张网上爬来爬去的蜘蛛咯,如果它遇到资源,那么它就会抓取下来.比如它在抓取一个网 ...

  3. Python 爬虫基础知识

    requests Python标准库中提供了:urllib.urllib2.httplib等模块以供Http请求,但是,它的 API 太渣了.它是为另一个时代.另一个互联网所创建的.它需要巨量的工作, ...

  4. 网络爬虫基础知识(Python实现)

    浏览器的请求 url=请求协议(http/https)+网站域名+资源路径+参数 http:超文本传输协议(以明文的形式进行传输),传输效率高,但不安全. https:由http+ssl(安全套接子层 ...

  5. Python归纳 | 爬虫基础知识

    1. urllib模块库 Urllib是python内置的HTTP请求库,urllib标准库一共包含以下子包: urllib.error 由urllib.request引发的异常类 urllib.pa ...

  6. 【VB6】使用VB6创建和访问Dom树【爬虫基础知识 】

    使用VB6创建和访问Dom树 关键字:VB,DOM,HTML,爬虫,IHTMLDocument 我们知道,在VB中一般大家会用WebBrowser来获取和操作dom对象. 但是,有这样一种情形,却让我 ...

  7. Scrapy爬虫学习笔记 - 爬虫基础知识

    一.正则表达式 二.深度和广度优先                                三.爬虫去重策略

  8. python 爬虫基础知识(继续补充)

    学了这么久爬虫,今天整理一下相关知识点,还会继续更新 HTTP和HTTPS HTTP协议(HyperText Transfer Protocol,超文本传输协议):是一种发布和接收 HTML页面的方法 ...

  9. 自学Python四 爬虫基础知识储备

    首先,推荐两个关于python爬虫不错的博客:Python爬虫入门教程专栏   和 Python爬虫学习系列教程 .写的都非常不错,我学习到了很多东西!在此,我就我看到的学到的进行总结一下! 爬虫就是 ...

随机推荐

  1. notepad++中设置python运行

    1. Notepad++ ->"运行"菜单->"运行"按钮 2. 在弹出的窗口内输入以下命令: cmd /k python "$(FULL ...

  2. Android 开发工具类 31_WebService 获取手机号码归属地

    AndroidInteractWithWebService.xml <?xml version="1.0" encoding="utf-8"?> & ...

  3. 工具类APP

    应用名称 工具S 英文名称 未填写 应用描述 工具类APP 英文描述 未填写 应用官网 this 应用图标  

  4. libnetwork插件化网络功能

    Docker把网络跟存储这两部分的功能实现都以插件化形式剥离出来,允许用户通过指令来选择不同的后端实现.这也是Docker希望构建围绕着容器的强大生态系统的一些积极的尝试.剥离出来的独立容器网络项目叫 ...

  5. free函数使用时的注意事项。

    free函数是我们在写C语言程序时常用的函数,但是使用时需要注意,一不小心很肯能会引起吐核. 注意:free函数与malloc()函数配对使用,malloc函数释放申请的动态内存.对于free(p)这 ...

  6. Oracle 12c 操作 CDB PDB

    CREATE TRIGGER open_all_pdbs AFTER STARTUP ON DATABASE BEGIN EXECUTE IMMEDIATE 'alter pluggable data ...

  7. 【ASP.NET Core】处理异常--转

    老周写的[ASP.NET Core]处理异常非常的通俗易懂,拿来记录下. 转自老周:http://www.cnblogs.com/tcjiaan/p/8461408.html 今天咱们聊聊有关异常处理 ...

  8. springMVC对于Controller返回值的可选类型

    2018-01-11 对于springMVC处理方法支持支持一系列的返回方式:  (1)ModelAndView (2)Model (3)ModelMap (4)Map (5)View (6)Stri ...

  9. css3的overflow-anchor

    overflow-anchor属性使我们能够选择退出滚动锚定,这是一个浏览器特性,旨在允许内容在用户当前的DOM位置上加载,而不需要在内容完全加载后更改用户的位置. 为何要有这个属性? 滚动锚定是一种 ...

  10. hadoop学习笔记(十一):MapReduce数据类型

    一.序列化 1 hadoop自定义了数据类型,在hadoop中,所有的key/value类型必须实现Writable接口.有两个方法,一个是write,一个是readFileds.分别用于读(反序列化 ...