1.安装 pip install BeautifulSoup42.导入模块 #!/usr/bin/env python from bs4 import BeautifulSoup #process html #from bs4 import BeautifulStoneSoup #process xml #import BeautifulSoup #all 建立对象:str初始化,经常使用urllib2或browser返回的html初始化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"><a href="http://example.com/elsie" class="sister" id="link1">Elsie</a> and they lived at the bottom of a well.</p> <p class="story">...</p> """ soup = BeautifulSoup(html_doc)3.使用 soup 就是BeautifulSoup处理格式化后的字符串, soup.title 获得的是title标签, soup.p 获得的是文档中的第一个p标签,要想获得全部标签,得用find_all函数。 find_all 函数返回的是一个序列,能够对它进行循环,依次获得想到的东西. get_text() 是返回文本,这个对每个BeautifulSoup处理后的对象获得的标签都是生效的。你能够试试 print soup.p.get_text() 实际上是能够得到标签的其余属性的,好比我要得到a标签的href属性的值, 可使用 print soup.a['href'],相似的其余属性,好比class也是能够这么获得的(soup.a['class']) 特别的,一些特殊的标签,好比head标签,是能够经过soup.head 获得,其实前面也已经说了。 如何得到标签的内容数组?使用contents 属性就能够 好比使用 print soup.head.contents, 就得到了head下的全部子孩子,以列表的形式返回结果,可使用 [num] 的形式得到 ,得到标签,使用.name 就能够。 获取标签的孩子,也可使用children,可是不能print soup.head.children 没有返回列表,返回的是 <listiterator object at 0x108e6d150>, 不过使用list能够将其转化为列表。固然可使用for 语句遍历里面的孩子。 关于string属性,若是超过一个标签的话,那么就会返回None,不然就返回具体的字符串print soup.title.string 就返回了 The Dormouse's story 超过一个标签的话,能够试用strings 向上查找能够用parent函数,若是查找全部的,那么可使用parents函数 查找下一个兄弟使用next_sibling,查找上一个兄弟节点使用previous_sibling,若是是查找全部的,那么在对应的函数后面加s就能够 如何遍历树? 使用find_all 函数 find_all(name, attrs, recursive, text, limit, **kwargs) 举例说明: print soup.find_all('title') print soup.find_all('p','title') print soup.find_all('a') print soup.find_all(id="link2") print soup.find_all(id=True) 返回值为: [<title>The Dormouse's story</title>] [<p class="title"><b>The Dormouse's story</b></p>] [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>] [<a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>] [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>] 经过css查找,直接上例子把: print soup.find_all("a", class_="sister") print soup.select("p.title") 经过属性进行查找 print soup.find_all("a", attrs={"class": "sister"}) 经过文本进行查找 print soup.find_all(text="Elsie") print soup.find_all(text=["Tillie", "Elsie", "Lacie"]) 限制结果个数 print soup.find_all("a", limit=2) 结果为: [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>] [<p class="title"><b>The Dormouse's story</b></p>] [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>, <a class="sister" href="http://example.com/tillie" id="link3">Tillie</a>] [u'Elsie'] [u'Elsie', u'Lacie', u'Tillie'] [<a class="sister" href="http://example.com/elsie" id="link1">Elsie</a>, <a class="sister" href="http://example.com/lacie" id="link2">Lacie</a>] 总之,经过这些函数能够查找到想要的东西。================================================= 最简单的爬虫,在Python3中,urllib库能够独立完成这些工做。下面是直接在python控制台上输入的代码最简单的爬虫,能够获取到原始字节及解码后的文本。 >>> import urllib.request as request >>> url = 'http://www.baidu.com' >>> origin_bytes = request.urlopen( url ).read() >>> origin_string = origin_bytes.decode( 'utf-8' ) >>> print(origin_string)其中origin_bytes如今获得了传输过来的原始字节,而origin_string则是将这些原始字节以UTF-8编码方式解码出来的原始字符串。而后咱们就可使用各类HTML解析工具或单纯地用文本解析工具来获取须要的数据。 使用headers假装成浏览器进行访问 有的网站好比说http://www.oschina.net,若是用上面那种方法来访问,则会返回403拒绝访问的错误信息。这是由于有部分网站禁止除浏览器以外的方法进行访问。 须要在发送请求的时候加入headers信息,假装成浏览器,这样就不会出现403的错误了。抓取频繁时还须要变化head信息和采用代理IP的方式。 下面是一个最简单的使用headers的例子。 >>> import urllib.request as request >>> url = 'http://www.oschina.net' >>> headers = ('User-Agent','Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11') >>> opener = request.build_opener() >>> opener.addheaders = [ headers ] >>> origin_bytes = opener.open( url ).read() >>> origin_string = origin_bytes.decode( 'utf-8' ) 其中headers能够从浏览器的调试工具中看到,好比firefox中的firebug,能够复制curl内容进行分析。