python 爬虫 使用xpath模块爬取猪八戒网站信息
# coding:utf-8
#1.拿到页面源代码
#2.提取和解析数据
import requests
from lxml import etree
import csv
url = "https://qingdao.zbj.com/search/f/?type=new&kw=saas"
resp=requests.get(url)
# print(resp.text)
html=etree.HTML(resp.text)
f = (open("zbj_data.csv", mode="w"))
csvwriter = csv.writer(f)
# print(html.xpath("/html/body/div[6]/div/div/div[2]/div[6]/div[1]/div"))#拿到一堆的服务商的div
divs=html.xpath("/html/body/div[6]/div/div/div[2]/div[6]/div[1]/div")
for div in divs:
price=div.xpath("./div/div[2]/div[2]/i/text()")
title = "saas".join(div.xpath("./div/div[2]/div[3]/a/text()"))
com_name = div.xpath("./div/div[1]/div[2]/section/h4/a/text()")
location = div.xpath("./div/div[1]/div[2]/section/div/span/text()")
csvwriter.writerow([price,title,com_name,location])
print(price)
print(title)
print(com_name)
print(location)
f.close()
print("over")