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("\n\n") print(comp_name,title,price)
实例来源:https://www.bilibili.com/video/BV1i54y1h75W?p=1