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 BeautifulSoupmodule’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 BeautifulSoupobject. 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的更多相关文章
随机推荐
- Android其它新控件 (转)
原文出处:http://blog.csdn.net/lavor_zl/article/details/51312715 Android其它新控件是指非Android大版本更新时提出的新控件,也非谷歌I ...
- 求二叉树的宽度C语言版
/*层次遍历二叉树,每一层遍历完成以后都重新插入特定的指针 (比如本例使用的特殊指针是数据元素为#,左右儿子为空的指针), 这样在每次访问到所指向数据为#的队列中的结点指针是就知道该指针是这层的末尾, ...
- 了解ASP.NET MVC几种ActionResult的本质:JavaScriptResult & JsonResult
在之前的两篇文章(<EmptyResult & ContentResult>和<FileResult>)我们剖析了EmptyResult.ContentResult和F ...
- Android 二维码 生成和识别(转)
原博客地址 :http://www.cnblogs.com/weixing/archive/2013/08/28/3287120.html 还有几个写的也可以参考一下:http://www.itnos ...
- iOS开发小技巧--TableView中headerView的循环利用,以及自定义的headerView
一.首先要搞清楚,tableView中有两种headerView,一个是tableHeaderView,另一个是headerView.前者就一个;后者根据session决定个数 headerView的 ...
- Linux_Shell_脚本参数接收键盘输入
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #!/bin/bash #提示"请输入姓名"并等待30秒,把用户的输入保存入变量name ...
- tomcat+javaWeb+spring的一个都市供求管理系统
这个作为自己学习javaweb的第一个小项目,也是跟着视频自己学的,是来自java1234的小锋写的,那边有很多java视频可以作为学习参考哦 , 视频中使用的是tomcat作为后端,也( •̀ ω ...
- jquery获取多重input的方式
获取input的checked值是否为true: 第一种: if($("input[name=item][value='val']").attr('checked')==true) ...
- SSM三大框架(转发)
转自:SSM三大框架整合详细教程(Spring+SpringMVC+MyBatis) 使用SSM(Spring.SpringMVC和Mybatis)已经有三个多月了,项目在技术上已经没有什么难点了,基 ...
- retinajs 使用方法
本文根据retinajs的官网翻译,如果有翻译错的地方,还请朋友指正.谢谢. 工作原理: 现在有4种方式: 1.自动交换“img”标签的"src"路径. 2.在内联样式中自动交换背 ...