zoukankan      html  css  js  c++  java
  • Python 使用BeautifulSoup


    pip install bs4
    pip install lxml

    BeautifulSoup 使用

    from bs4 import BeautifulSoup

    page_text=res.text
    soup=BeautifulSoup(page_text,'lxml')

    数据解析方法:
    soup.tagName: 返回文档中,第一次出现的tagName
    soup.find()
    |-soup.find('tagName') 等同于 soup.div
    |-属性定位:
    soup.find('div',class_/id/attr="mao")
    soup.find_all('tagName') 符合所有标签

    -select:
    |- select('某种选择器(id,class,标签,……)'),返回一个列表
    |-层级选择器:
    -soup.select('.mao>ul>li>a') >表示的是一个层级
    -soup.select('.mao>ul a') 空格表示的多个层级


    -获取标签之间的文本数据:
    -soup.a.text/string/get_text()/soup.a['href']
    -text/get_text():获取标签中所有的文本内容
    -string: 只可以获取标签下面直系的文本内容

    专注:全栈开发工程师 | 独立开发者
  • 相关阅读:
    [ASP.NET Core] Tips
    Integration_Unit test coding standard
    集成测试报错的解决方案
    Integration testing
    Web Cache
    BIT
    CSU 1449: A+B and C
    [转] CUDA + code::blocks 配置
    CF 245 div2
    NBUT 2014 C Lord of Minecraft
  • 原文地址:https://www.cnblogs.com/wookong/p/15164335.html
Copyright © 2011-2022 走看看