1. 程式人生 > 其它 >使用Xpath屠戮豬八戒網

使用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)

結果展示: