目 录CONTENT

文章目录

pandas+request

Administrator
2024-08-26 / 0 评论 / 5 点赞 / 42 阅读 / 0 字
import requests
import pandas as pd

s = requests.Session()
headers = {
    "Content-Type": "application/json",
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36 SLBrowser/9.0.3.5211 SLBChan/105"
}

# 获取小区名称的函数
def pa_name(x):
    xq = s.get(f'https://bj.lianjia.com/xiaoqu/{x}/',headers=headers)
    name = xq.text.split('detailTitle">')[1].split('</h1>')[0]
    return name
# 获取平均房价的函数
def pa_price(x):
     xq = s.get(f'https://bj.lianjia.com/xiaoqu/{x}/',headers=headers)
     price = xq.text.split('xiaoquUnitPrice">')[1].split('</span>')[0]
     return int(price)

# 小区列表
xqs =[1111027377595, 1111027382589,
      1111027378611, 1111027374569,
      1111027378069, 1111027374228,
      116964627385853]

# 构造数据
df = pd.DataFrame(xqs, columns=['小区'])
# 爬取小区名
df['小区名'] = df.小区.apply(lambda x: pa_name(x))
# 爬取房价
df['房价'] = df.小区.apply(lambda x: pa_price(x))

print(df)

# 使用Styler对象高亮显示"房价"列的最小值
styled_df = df.head().style.highlight_min(subset=['房价'])

# 将多个df分不同sheet导入一个Excel文件中
with pd.ExcelWriter('房价分析测试.xlsx') as writer:
     styled_df.to_excel(writer, sheet_name='房价分析',index=False)
     styled_df.to_excel(writer, sheet_name='房价分析副本',index=False)

5
  1. 支付宝打赏

    qrcode alipay
  2. 微信打赏

    qrcode weixin

评论区