嗨喽~大家好呀,这里是魔王呐 !
链家二手房数据:多页《—一页
发送请求,向目标网址发送数据请求
获取网页源代码<响应文本信息>
解析数据
保存数据
from lxml import etree
import csv
import requests
因CSDN不能出现网站链接,所以我把下图代码删了,大家可以按照下图片把它添加一下
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36'
}
html = requests.get(url=url, headers=headers)
# print(html.text)
# 解析 转换数据类型
soup = etree.HTML(html.text)
# print(soup)
doc = soup.xpath('//*[@id="content"]/div[1]/ul/li')
# print(doc)
list_1 = []
for fang in doc:
name = fang.xpath('.//div[@class="title"]/a/text()')[0]
# print(len(name))
# print(name)
address = fang.xpath('.//div[@class="positionInfo"]/a/text()')
# print(len(address))
# print(address)
# for i in range(len(address)):
if address:
address = '-'.join(address)
# print(address)
# 价格
price = fang.xpath('.//div[@class="totalPrice totalPrice2"]/span/text()')[0]
# print(price)
list_1.append([name, address, float(price)])
print(list_1)
for p in list_1:
list_2 = p
print(list_2)
file = open('ershoufang.csv', mode='a', newline='')
csv_write = csv.writer(file)
csv_write.writerow(list_2)
import pandas as pd
from pyecharts.charts import Bar
from pyecharts import options
data = pd.read_csv('./ershoufang.csv', names=['name', 'address', 'price'], encoding='gbk')
print(data)
bar = Bar()
bar.add_xaxis(list(data['address']))
bar.add_yaxis('长沙二手房房价图', list(data['price']))
bar.set_global_opts(
title_opts=options.TitleOpts(title="长沙二手房价图表"),
datazoom_opts=options.DataZoomOpts()
)
bar.render('长沙二手房房价图.html')
要成功,先发疯,下定决心往前冲!
学习是需要长期坚持的,一步一个脚印地走向未来!
未来的你一定会感谢今天学习的你。
—— 心灵鸡汤
本文章到这里就结束啦~感兴趣的小伙伴可以复制代码去试试哦