python爬虫---链家网二手房价采集

代码:

import requests
from lxml import etree
import pandas as pd
from pyecharts.charts import Bar
from pyecharts.charts import WordCloud
from pyecharts import options as opts
import os

# resp = requests.get("https://sjz.lianjia.com/ershoufang/").text  ---->str
from pyecharts.globals import ThemeType

resp = requests.get("https://sjz.lianjia.com/ershoufang/").content.decode()  # ---->bytes 字节
html = etree.HTML(resp)
div_list = html.xpath(".//div[@class='info clear']")

# 设置一个空列表保存小区和小区房价
ershoufang = []
for div in div_list:
    # 小区名称
    name = div.xpath(".//div[@class='positionInfo']/a/text()")[0]
    # 小区单价
    price = div.xpath(".//div[@class='unitPrice']/span/text()")[0].replace("元/平", "").replace(",", "")
    ershoufang.append([name, float(price)])
print(ershoufang)
title = ['小区名称', '元/平']
table = pd.DataFrame(ershoufang, columns=title)
bar = Bar(init_opts=opts.InitOpts(width="100%",theme=ThemeType.LIGHT))
bar.add_xaxis(list(table["小区名称"]))
bar.add_yaxis("石家庄二手房价信息",list(table["元/平"]))
bar.set_series_opts(label_opts=opts.LabelOpts(is_show=True))
bar.set_global_opts(title_opts=opts.TitleOpts(title="石家庄二手房价信息"))
bar.render("ershoufang.html")
os.system("ershoufang.html")

 

posted @ 2021-12-23 15:18  睡觉不困  阅读(126)  评论(0)    收藏  举报