zoukankan      html  css  js  c++  java
  • python爬虫笔记(2-2)xpath解析实例

    # xpath抓取猪八戒网
    # 1、拿到页面源代码
    # 2、解析和提取数据
    import requests
    from lxml import etree
    
    # 拿到页面源码
    url = "https://nanjing.zbj.com/search/f/?type=new&kw=saas&fr=search-history"
    resp = requests.get(url)
    resp.encoding = "utf-8"
    # print(resp.text)
    
    # 解析数据
    html = etree.HTML(resp.text)
    # 提取数据
    # 提取每个服务商的div
    divs = html.xpath("/html/body/div[6]/div/div/div[2]/div[5]/div[1]/div")
    # print(divs)
    for div in divs:
        price = div.xpath("./div/div/a[1]/div[2]/div[1]/span[1]/text()")[0].strip("¥")
        # title = div.xpath("./div/div//a[1]/div[2]/div[2]/p/text()")
        title = "SAAS".join(div.xpath("./div/div//a[1]/div[2]/div[2]/p/text()"))
        comp_name = div.xpath("./div/div/a[2]/div[1]/p/text()")[1].strip("
    
    ")
        print(comp_name,title,price)

     实例来源:https://www.bilibili.com/video/BV1i54y1h75W?p=1

  • 相关阅读:
    load custom class in drupal 8
    HEAD in Git
    composer version constraint 版本号前缀
    如何测试
    看待一段数据
    创建一个plugin
    eclipse的快捷方式
    .git文件夹的猜想
    本地可以但远程不行
    方法点不进去的原因
  • 原文地址:https://www.cnblogs.com/testerhappy/p/15117007.html
Copyright © 2011-2022 走看看