Python教程

python爬虫笔记(2-1)xpath抓猪八戒

本文主要是介绍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抓猪八戒的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!