Bootstrap

python爬取58同城的新房信息

以长沙为例 ,用python爬取58同城中的新房信息,包含楼盘名称、均价。

import requests
from lxml import etree

headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36 Edg/85.0.564.67'
        }
url='https://cs.58.com/xinfang/'
#获取响应数据
page_text=requests.get(url=url,headers=headers).text
#数据解析
tree=etree.HTML(page_text)
#存储的就是li标签对象
list_text=tree.xpath('//div[@class="key-list imglazyload"]/div')
print(list_text)
fp=open('58.txt','w',encoding='utf-8')
for div in list_text:
    title_name=  div.xpath('./div/a[1]/span/text()')[0]
    title_price0=div.xpath('./a[@class="favor-pos"]/p//text()')
    title_price1=",".join(title_price0)
    fp.write(title_name+":" +title_price1 +'\n')
    print(title_name+":" +title_price1 +'\n')

;