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的更多相关文章

随机推荐

  1. [转]Servlet 中文乱码问题及解决方案剖析

    原文地址:http://blog.csdn.net/xiazdong/article/details/7217022/ 一.常识了解 1.GBK包含GB2312,即如果通过GB2312编码后可以通过G ...

  2. for循环 打印菱形 空 和 实

    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><?ph ...

  3. python协程和yeild

    python多线程其实在操作系统级别是进程,因为在执行时,默认加了一个全局解释器锁(GIL),python的多线程,本质还是串行的,无法利用多核的优势:在java和C# 中,多线程是并发的,可以充分利 ...

  4. BZOJ 1123: [POI2008]BLO

    1123: [POI2008]BLO Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1030  Solved: 440[Submit][Status] ...

  5. java项目的划分方式:模块优先还是层优先?

    I've seen and had lots of discussion about "package by layer" vs "package by feature& ...

  6. 一次性下载CVPR2016的所有文章

    wget --no-clobber --convert-links --random-wait -r -p -E -e robots=off -U mozilla http://www.cv-foun ...

  7. JSR303注解

    Annotation 属于Bean Validation 规范 应用位置 作用 对Hibernate Core中的元数据的影响 @AssertFalse yes field/property 检查被标 ...

  8. ng-if else的使用

    <!DOCTYPE html> <html> <head> <script src="http://code.angularjs.org/1.2.0 ...

  9. RAC 相关概念解释

    1.1 并发控制 在集群环境中, 关键数据通常是共享存放的,比如放在共享磁盘上. 而各个节点的对数据有相同的访问权限, 这时就必须有某种机制能够控制节点对数据的访问. Oracle RAC 是利用DL ...

  10. Android成长日记-Fragment的生命周期与Activity通信

    1. public void onAttach(Activity activity) 当Fragment被添加到Activity时候会回调这个方法,并且这个方法只会被回调一次 2. public vo ...