使用Xpath屠戮猪八戒网

import requests
from lxml import etree
#需求分析:使用xpath爬取猪八戒网
#需要爬取的信息有公司名称、地点、价格、标题
headers = {
‘User-Agent‘: ‘Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36‘
}
def get_info(url):
url = ‘https://xian.zbj.com/search/f/?kw=logo设计
res = requests.get(url,headers=headers)
html = etree.HTML(res.text)
divs = html.xpath(‘/html/body/div[6]/div/div/div[3]/div[5]/div/div‘)
for div in divs:
try:

com_name = div.xpath(‘./div/div//a[1]/div[1]/p/text()‘)[1].strip() #公司名称
address = div.xpath(‘./div/div//a[1]/div[1]/div/span/text()‘)[0] #地点
price = div.xpath(‘./div/div//a[2]/div[2]/div[1]/span[1]/text()‘)[0].strip("¥") #价格
title =‘logo设计‘.join( div.xpath(‘./div/div//a[2]/div[2]/div[2]/p/text()‘)) #标题
data = {
"公司名称" : com_name,
"地点" : address,
"价格" : price,
"标题" : title
}
print(data)
except IndexError:
pass
if __name__ == ‘__main__‘:
url = ‘https://xian.zbj.com/search/f/?kw=logo设计
get_info(url)

结果展示:
使用Xpath屠戮猪八戒网

使用Xpath屠戮猪八戒网

上一篇:echart-loading-柱状图设置渐变色


下一篇:Longhorn,企业级云原生容器分布式存储 - 定制默认设置