zoukankan      html  css  js  c++  java
  • bs4_2

    python机器学习-乳腺癌细胞挖掘(博主亲自录制视频)https://study.163.com/course/introduction.htm?courseId=1005269003&utm_campaign=commission&utm_source=cp-400000000398149&utm_medium=share

     

    机器学习,统计项目合作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文件名报存
     
  • 相关阅读:
    GridView中绑定日期字段格式的定义《收集》
    纯代码实现GridView绑定增删改
    开发人员一定要加入收藏夹的网站《收藏》
    ASP.NET 2.0 创建母版页导致js出现“ 'document.getElementById(...)' 为空或不是对象”错误《转》
    ModelSim SE 6.5破解
    [转载]ZIGBEE:Coordinator中的邻居表(Neighbour Table)问题
    【转载】在ZigBee网络中实现节电断电之后重新加入网络
    Zstack 串口的使用
    PAN_ID
    ZigBee四种绑定 在TI ZStack和谈栈中应用
  • 原文地址:https://www.cnblogs.com/webRobot/p/5236665.html
Copyright © 2011-2022 走看看