python爬虫笔记(2-1)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 = "UI设计".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)

 

上一篇:python爬虫笔记(2-1)Xpath解析


下一篇:简单的python爬虫代码,python爬虫代码大全