青客是5.15爬出來的备恤,事實證明做完一個案例就寫下來是很有必要的编整,因為才3天我就不記得當時的糾結了...心態(tài)太好也比較容易失憶的找御!
總之這個是比較好爬的元镀,雖然它的代碼寫的有點怪,什么fR霎桅,easyCon之類栖疑,不過這些都是表象!
就是老老實實的用手把其中一個房源的列表li下面的都寫下來滔驶,要爬的幾個點:價格遇革,描述,會員價用紅筆劃出來)
圖片發(fā)自簡書App
再老老實實的把每層結構都寫下來揭糕÷芸欤——我居然不記得[@class="..."]要用中括號括起來,被程工指出了著角。反正我也經常指出人家用了中文符號當英文這種錯誤揪漩,所以我很輕易的原諒了自己。
網站截圖:
3.png
代碼
#coding:utf-8
import requests
from lxml import etree
import random
url='http://hz.qk365.com/list/p1'
def getReqHeaders():
"""
功能:隨機獲取HTTP_User_Agent
"""
user_agents=[
"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36"]
user_agent = random.choice(user_agents)
req_headers={'User-Agent':user_agent}
return req_headers
html=requests.get(url,headers=getReqHeaders()).content
selector=etree.HTML(html)
infos=selector.xpath('//*[@class="easyList"]/li')
list_dict=[]
for info in infos:
item={}
price=info.xpath('div[@class="coverBox"]/em[@class="fR"]/b/text()')[0]
address=info.xpath('div[@class="easyCon"]/p[@class="easySub"]/text()')[0]
price2=info.xpath('div[@class="easyCon"]/div[@class="easyMember"]/span/i/text()')[0]
print price, ' ',address, ' ',price2
#item['name']=name
#item['address']=address
#item['price']=price
#list_dict.append(item)
#print list_dict
這里只是第一頁吏口,也沒做城市列表奄容。本來想收拾一下的冰更,但已經失去挑戰(zhàn)感了。就這樣存稿吧嫩海。