zoukankan      html  css  js  c++  java
  • xPath,beautifulsoup和pyquery

    一、XPath

    from lxml import etree

    html = etree.parse('html源代码',etree.HTMLPaser())

    1.节点的获取

      a.html.xpath('//*')  #匹配HTML中的所有节点

      b.html.xapth('//li') #匹配所有的li节点

      c.// 获取所有的子孙节点   ‘/’  获取子节点(区别好 '//'  与  '/')

      d.html.xapth('//li[@class='item-0']')  #根据属性匹配

      e.html.xapth('//li[contains(@class,'li')])  #属性多值匹配 contains()方法,第一个参数传入属性名称,第二个参数传入属性值,只要此属性包含所传入的属性值就可以完成匹配

      f.多属性匹配  .html.xapth('//li[contains(@class,'li') and @name='item'])

      g.按顺序选择 html.xapth('//li[last()]') 最后一个li节点            

         html.xapth('//li[position()<3]') 位置小于3的li节点        

         html.xapth('//li[1]')  选择第一个li节点

      h.节点轴选择 html.xapth('//li/child::')  直接子节点      

         html.xapth('//li/ancestor::') 所有祖先节点         

       html.xapth('//li'/attribute::)  获取所有的属性值

       html.xapth('//li/decendant::') 获取所有的子孙节点

      2.属性,文本获取

        (1).文本获取

          html.xapth('//li/text()')

        (2).属性获取

          html.xapth('//li/@class')  #获取li标签的class的属性值

    二、BeautifulSoup

    1.节点选择器

      直接调用节点名称就可以选择节点元素,在调用string属性就可以得到节点内的文本

      eg:soup.title.string

      a.提取信息:可以通过name属性获得节点的名称      soup.title.name

      b.获取属性:每个节点有多个属性,比如 id,class等。选择这个节点元素之后,可以调用attrs获取所有的属性    soup.p.attrs    

           soup.p.attrs[‘name’]  获取属性name的值      另一种简单的方式:soup.p['name']

      c.获取内容:soup.p.string   获取p标签的内容

    2.嵌套选择:比如我们之前获取了head的节点元素,我们可以继续调用head来选去head的内部元素

    3.关联选择

      a.子节点与子孙节点:  soup.p.contents 获取p标签元素的所有直接子节点,返回的是列表

                  soup.p.children 也是返回p标签元素的所有直接子节点, 不过返回的是生成器

                  soup.p.descendants 返回p标签的所有的子孙节点,返回的是生成器

      b.父节点和祖先节点

                  soup.p.parent  返回的是p标签的直接父节点

                  soup.p.parents 返回的是p标签的所有祖先节点,返回的是生成器

      c.兄弟节点  

                  soup.p.next_slibling  返回的是节点的下一个节点

                  soup.p.next_sliblings      返回的是下面的所有的兄弟节点,返回的是生成器

                  soup.p.previous_slibling  返回的是上一个兄弟节点

                  soup.p.previous_slibling  返回的是上面的所有的兄弟节点,返回的生成器

    4.方法选择器

      a.find_all()   查找所有符合的元素,返回的是列表     find_all(name,attrs,recursive,tetxt,**kwargs)

        name:根据节点名称查询元素  soup.find_all(name='ul')   查询文档树中所有的ul的标签元素

        attrs:根据属性查询节点   soup.find_all(attr={'id':'list-1'})  查询文档树中所有id属性值为‘list-1’的元素

        text:   根据文本查询节点 传入的的形式可以是字符串,也可以是正则表达式对象   soup.find_all(text=re.compile('link'))   查询文本中所有含link的标签元素

      b.find()  find 返回的生死单个元素,也就是匹配的第一个元素,方法同find_all()

      c. find_parents(),find_parent(),find_next_siblings(),find_next_sibling(),find_previous_siblings(),find_next_sibling(),find_all_next(),find_next(),find_all_prevous(),find_privous()

       这一组方法都是和find_all()与find()一致

    5.CSS选择器

      使用CSS选择器时,只需要调用select() 方法,传入相应的CSS选择器即可

      soup.select('CSS选择器')

    三、pyquery

    import pyquery as pq

    doc = pq(html文档or url)

    1.基本CSS选择器

      doc('CSS选择器')

    2.查找节点  

      a.查找子节点:find()方法  find()方法查找的是所有的子孙节点,如果只想查找子节点,可以用children()方法;find()或者children()方法直接传入CSS选择器即可

      b.查找父节点:parent()  查询直接父节点,parents() 查找祖先节点    都是传入CSS选择器即可

      c.兄弟节点:siblings() 筛选摸个兄弟节点可以传入CSS选择器

    3.遍历:对于多个节点的结果,就需要遍历,需要调用items()方法  doc('li').items()   遍历所有的li标签元素

    4.获取信息

      a.获取属性   提取到节点之后,就可以调用attr()方法获取属性  doc('.item-0.active a').attr('href')  

              也可以通过调用attr属性来获取属性    doc('.item-0.active a').attr.href  (说明:当返回结果包含多个节点时,调用attr()方法智慧得到第一个属性值)

      b获取文本内容:调用text()方法   doc('.item-0.active a').text()

      c.获取HTML文本 ,调用html()方法   doc('.item-0.active a').html()

    5.节点操作

      a.addClass(),removeClass() 动态的改变节点的class的属性值

      b.attr,text,html 修改属性值,文本内容,html文本

         doc('.item-0.active a').attr('name','link'); doc('.item-0.active a').text('changed item');   doc('.item-0.active a').html('<span> change item</span>')

      c.remove 移除,可以将特定的节点移除   doc('.item-0.active a').remove()

    6.伪类选择器(举例说明):

      a. li = doc('li:first-child')

      b. li = doc('li:last-child')

      c. li = doc('li:nth-child(2)')

      d. li = doc('li:gt(2)')

      e .li = doc('li:nth-child(2n)')

      f. li = doc('li:contain(second))

    好了就写到这了!!!!!!!!!!!!!!!!!!!!!!!

  • 相关阅读:
    洛谷 P4484
    洛谷 P4900
    Codeforces 1500D
    Codeforces 1322D
    2021.9.30 Codeforces 中档题四道
    BZOJ 3729
    洛谷 P6276
    Codeforces 1511G
    C语言 typedef
    C语言 回调函数 callback
  • 原文地址:https://www.cnblogs.com/mdevelopment/p/9379376.html
Copyright © 2011-2022 走看看