bs4_2
QQ:231469242
欢迎交流
Parsing HTML with the BeautifulSoup Module
Beautiful Soup是用于提取HTML网页信息的模板,BeautifulSoup模板名字是bs4。
bs4.BeautifulSoup()函数需要调用时,携带包含HTML的一个字符串。这个字符串将被复制。
bs4.BeautifulSoup()返回一个BeautifulSoup对象。
Beautiful Soup is a module for extracting information from an HTML page (and is much better for this purpose than regular expressions). The BeautifulSoup
module’s name is bs4
(for Beautiful Soup, version 4). To install it, you will need to run pip install beautifulsoup4
from the command line. (Check out Appendix A for instructions on installing third-party modules.) While beautifulsoup4
is the name used for installation, to import Beautiful Soup you run import bs4
.
For this chapter, the Beautiful Soup examples will parse (that is, analyze and identify the parts of) an HTML file on the hard drive. Open a new file editor window in IDLE, enter the following, and save it as example.html. Alternatively, download it from http://nostarch.com/automatestuff/.
<!-- This is the example.html example file. -->
<html><head><title>The Website Title</title></head>
<body><p>Download my <strong>Python</strong> book from <a href="http://inventwithpython.com">my website</a>.</p><p class="slogan">Learn Python the easy way!</p><p>By <span id="author">Al Sweigart</span></p>
</body></html>
As you can see, even a simple HTML file involves many different tags and attributes, and matters quickly get confusing with complex websites. Thankfully, Beautiful Soup makes working with HTML much easier.
Creating a BeautifulSoup Object from HTML
bs4.BeautifulSoup()函数需要调用时,携带包含HTML的一个字符串。这个字符串将被复制。
bs4.BeautifulSoup()返回一个BeautifulSoup对象。
The bs4.BeautifulSoup()
function needs to be called with a string containing the HTML it will parse. The bs4.BeautifulSoup()
function returns is a BeautifulSoup
object. Enter the following into the interactive shell while your computer is connected to the Internet:
>>> import requests, bs4
>>> res = requests.get('http://nostarch.com')
>>> res.raise_for_status()
>>> noStarchSoup = bs4.BeautifulSoup(res.text) #返回文字属性给bs4.BeautifulSoup函数
>>> type(noStarchSoup)
<class 'bs4.BeautifulSoup'>
This code uses requests.get()
to download the main page from the No Starch Press website and then passes the text
attribute of the response to bs4.BeautifulSoup()
. The BeautifulSoup
object that it returns is stored in a variable named noStarchSoup
.
You can also load an HTML file from your hard drive by passing a File
object tobs4.BeautifulSoup()
. Enter the following into the interactive shell (make sure theexample.html file is in the working directory):
>>> exampleFile = open('example.html')
>>> exampleSoup = bs4.BeautifulSoup(exampleFile)
>>> type(exampleSoup)
<class 'bs4.BeautifulSoup'>
Once you have a BeautifulSoup
object, you can use its methods to locate specific parts of an HTML document.
如何创建一个example.html测试文件
打开一个idle文件,复制好下图HTML代码,用example.html文件名报存
bs4_2的更多相关文章
随机推荐
- 【原创】解决jquery在ie中不能解析字符串类型xml结构的xml字符串的问题
$.fn.extend({ //此方法解决了ie中jquery不识别非xml的类型的xml字符串的问题 tony tan findX: function (name) { if (this & ...
- zabbix3.0安装教程
一.Zabbix介绍 zabbix 简介 Zabbix 是一个高度集成的网络监控解决方案,可以提供企业级的开源分布式监控解决方案,由一个国外的团队持续维护更新,软件可以自由下载使用,运作团队靠提供收费 ...
- json写入new_hello文件
#写入new文件 import json dic = {'name':'alex'} i = 8 s = 'hello' l = [11,22] f = open("new_hello&qu ...
- 100722A
这道题抄了答案: 思路:旋转,其实只用旋转四次,因为在换行的过程中旋转其实是没有意义的,因为行列只不过转了个角度.然后主要的是行列的交换,这里我很头疼,写了个盲目搜索,当然wa掉了 问了问某位同志,是 ...
- git初体验(一)基础
一.window下的git安装 1.安装教程 网上教程一堆,我参考的是这个:Git_Windows 系统下Git安装图解 还有这个也不错 2.环境搭建: 在配置完成后,自动加载到系统环境变量中,如我的 ...
- 使用jmeter进行性能测试-Jmeter教程及技巧汇总 (转)
http://www.jmeter.cf/loadtesting-jmeter.html 为什么使用jmeter, 它免费开源, 不断发展, 功能逐渐强大. 可以做功能,负载, 性能测试.一套脚本可以 ...
- 【POJ 1698】Alice's Chance(二分图多重匹配)
http://poj.org/problem?id=1698 电影和日子匹配,电影可以匹配多个日子. 最多有maxw*7个日子. 二分图多重匹配完,检查一下是否每个电影都匹配了要求的日子那么多. #i ...
- BZOJ 1105: [POI2007]石头花园SKA
1105: [POI2007]石头花园SKA Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 628 Solved: 182[Submit][Stat ...
- 【VS2013】设定Nuget代理
@tags "visual studio 2013" nuget vs2013中用nuget想必是一件很爽的事情,就像java里面用maven来安装各种包一样.有时候网络不好,nu ...
- linux中/和/root(~) 和 /home
winodws是森林型目录结构,它有很多根,如C.D.E.F等都是它的根目录,然后在其实创建子目录linux是树型目录结构,它只有一个根就是/目录,然后在/目录在有子目录如/root./home./e ...