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'  。

最新文章

  1. CSharpGL(23)用ComputeShader实现一个简单的ParticleSimulator
  2. oracle与mysql创建表时的区别
  3. windows下新安装的mysql修改root password问题
  4. 使用JS脚本获取url中的参数
  5. javascript定时器,取消定时器,及js定时器优化方法
  6. HDU 4946 Area of Mushroom (几何凸包)
  7. C结构体中数据的内存对齐问题
  8. PHP 打印调用函数入口地址(堆栈)
  9. python备忘录
  10. Ubuntu16.04下Intellij IDEA不能输入中文的问题
  11. [IOS ] - INFO
  12. Asp.net MVC + EF + Spring.Net 项目实践(一)
  13. spring mvc 入门示例
  14. Nodejs进阶:服务端字符编解码&amp;乱码处理
  15. Java工程读取resources中资源文件路径问题
  16. TZOJ 5101 A Game(区间DP)
  17. VB Byte数组转字符串问题
  18. UI5-学习篇-16-云端SCP-Destination配置
  19. 学习Java的方法
  20. 记一次生产发版时SpringBoot服务停用启用的问题

热门文章

  1. 点击键盘上的“Next”button实现文本框焦点跳转
  2. My97datepicker使用方法
  3. C# - 静态类和静态构造函数
  4. POJ 2352 stars (树状数组入门经典!!!)
  5. jFinal 2.2入门学习之一:搭建框架输出helloword
  6. tomcat启动错误org.springframework.beans.factory.CannotLoadBeanClassException的解决
  7. C++程序设计入门(上) 函数学习
  8. MySQL5.7.24安装笔记
  9. MySQL----MySQL数据库入门----第四章 单表查询
  10. tp5 的nginx配置