• 周五. 12月 2nd, 2022

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

python爬虫笔记(2-2)xpath解析实例

admin

11月 28, 2021
# 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

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注