# -*- coding: utf-8 -*-
import requests
from lxml import etree

headers={
        'user-agent':'Mozilla/5.0 (Windows NT 6.1; ) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/82.0.4068.4 Safari/537.36'
        }
def fang_spider():
    url='https://cs.newhouse.fang.com/house/s/';
    resp=requests.get(url,headers=headers)
    resp_text=resp.content.decode(encoding='gbk')
    #print(resp_text)#编码 asc-gbk-utf-8
    resp_text_html=etree.HTML(resp_text)#扶桑
    resp_list=resp_text_html.xpath('//div[@class="nl_con clearfix"]/ul/li')
    names=[]
    prices=[]
    
    for i in resp_list:
        name=i.xpath('.//div[@class="nlcd_name"]/a/text()')
        price=i.xpath('.//div[@class="nhouse_price"]/span/text()')
        if name !=[] and price !=[] and price !=['价格待定']:
            names.append(name[0].strip())
            prices.append(price[0].strip())
    #print(names)
    #print(prices)
    return names,prices
    
    
names,prices=fang_spider()
print(names)
print(prices)
#数据可视化
from pyecharts.charts import Bar
import pyecharts.options as opts


bar=Bar()
bar.add_xaxis(names)
bar.add_yaxis('长沙房价图',prices)
bar.set_global_opts(
        xaxis_opts=opts.AxisOpts(
                axislabel_opts=opts.LabelOpts(rotate=-40)
                ),#设置x轴标签旋转角度
        yaxis_opts=opts.AxisOpts(name='价格(元/平方米)'),
        title_opts=opts.TitleOpts(title='柱状图')
        )
bar.render('房价图.html')