鱼C论坛

 找回密码
 立即注册
查看: 903|回复: 5

[已解决]各位大佬看看小弟哪里出问题了 刚学

[复制链接]
发表于 2020-8-3 12:54:46 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
# -*- coding: utf-8 -*-
"""
Created on Sun Mar  1 19:12:53 2020

@author: acliu
"""

import xlwt
import requests
from bs4 import BeautifulSoup

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36',\
    "Host": "bj.ke.com",
    }   

# 将获取的信息保存到表格中
def save_info(content):
    workbook = xlwt.Workbook(encoding = 'ascii')
    worksheet = workbook.add_sheet('house info')
    style = xlwt.XFStyle() # 初始化样式
    font = xlwt.Font() # 为样式创建字体
    font.name = 'Times New Roman'
    font.bold = True # 黑体
    font.underline = True # 下划线
    font.italic = True # 斜体字
    style.font = font # 设定样式
    worksheet.write(0, 0, '名称')
    worksheet.write(0, 1, '位置')
    worksheet.write(0, 2, '房屋信息')
    worksheet.write(0, 3, '总价(万)')
    worksheet.write(0, 4, '单价(元/平方米)')
   
    for i, item in enumerate(content):
        for j in range(5):  #多添加一列(序号)
            worksheet.write(i+1, j, content[j])
    workbook.save('./house_info.xls') # 保存文件

# 获取房屋相关的信息
# 主要包括:title positon houseinfo totalprice unitprice
def get_info():
    all_info        = []
    title_list      = []
    position_list   = []
    house_list      = []
    totalPrice_list = []
    unitPrice_list  = []
   
    for i in range(3):
        link = 'https://cd.ke.com/xiaoqu/damian/pg%dl2/' % i
        r = requests.get(link, headers=headers, timeout=10)
        print (str(i+1), 'status_code: ', r.status_code)
        soup = BeautifulSoup(r.text, 'lxml')
        titleInfo = soup.findAll('div', {'class': 'info'})
        positionInfo = soup.findAll('div', {'class': 'positionInfo'})
        houseInfo = soup.findAll('div', {'class': 'houseInfo'})
        totalPrice = soup.findAll('div', {'class': 'totalPrice'})
        unitPrice = soup.findAll('div', {'class': 'unitPrice'})
        for item in titleInfo:
            title = item.div.a.text.strip()
            title_list.append(title)
        for item in positionInfo:
            postion = item.a.text.strip()
            position_list.append(postion)
        for item in houseInfo:
            house = item.text.strip().replace('\n', ' ').replace(' ', '')
            house_list.append(house)
        for item in totalPrice:
            total_price = item.span.text.strip()
            totalPrice_list.append(total_price)
        for item in unitPrice:
            unit_price = item.span.text.strip().replace('单价', '').replace('元/平米', '')
            unitPrice_list.append(unit_price)
    print (len(title_list))
    print (len(position_list))
    print (len(house_list))
    print (len(totalPrice_list))
    print (len(unitPrice_list))
    for i in range(len(title_list)):
        item = [title_list, position_list, house_list, totalPrice_list, unitPrice_list]
        all_info.append(item)

    return all_info


if __name__ == "__main__":
    all_info = get_info()
    save_info(all_info)
11111.png
最佳答案
2020-8-3 13:57:30



源码中根本找不到 unitPrice 这个节点属性,所以这个提取的数据为空:

22.png


想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-8-3 12:55:46 | 显示全部楼层


索引超出范围导致的报错

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-8-3 12:57:29 | 显示全部楼层
Twilight6 发表于 2020-8-3 12:55
索引超出范围导致的报错

请问下怎么解决这个问题呢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-8-3 13:01:10 | 显示全部楼层
L嘉 发表于 2020-8-3 12:57
请问下怎么解决这个问题呢





打印了长度和内容,发现最后一个是空列表,数据提取 unitPrice 出错了,是个空的提取内容,你重新写下 :unitPrice = soup.findAll('div', {'class': 'unitPrice'})  这个代码

33.png

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2020-8-3 13:43:11 | 显示全部楼层
Twilight6 发表于 2020-8-3 13:01
打印了长度和内容,发现最后一个是空列表,数据提取 unitPrice 出错了,是个空的提取内容,你重 ...

改在哪个位置啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-8-3 13:57:30 | 显示全部楼层    本楼为最佳答案   



源码中根本找不到 unitPrice 这个节点属性,所以这个提取的数据为空:

22.png


想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-27 07:54

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表