爬取链家租房信息

  1. 通过观察 发现url为 https://nb.lianjia.com/zufang/pg{}/ 其中{}中为页码 即1,、2、3、4…
    一共有100页,所一设置一个循环来产生这些url
for x in range(1, 101):
    url = 'https://nb.lianjia.com/zufang/pg%d/' % x
  1. 通过浏览器的开发者工具栏可知 该 url的请求的方式为get请求
  2. 请求得到源码
 response = requests.get(url, headers=headers)
  1. 利用lxml将其解析为规范的html格式,并解析得到每条信息的div标签
html = etree.HTML(result, etree.HTMLParser())
divs = html.xpath("//div[@class='content__list--item']")
  1. 由于数据量很大 所以没办法保证每一条数据都获取到全部的内容,所以为了保证代码的健壮性,加入try语法,将获取不到的数据置为None。
    title = None
    house_url = None
    local = None
    area = None
    home = None
    price = None
    label = None
    orientation = None
  1. 通过分析网页源代码,通过xpath语法,按一定的匹配规则将所需的内容匹配出来; 在获取数据的时候,由于每条数据的内容存在差异,进一步进行处理,获取更完整的数据
    for div in divs:
        try:
            title = "".join(div.xpath(".//p[contains(@class,'content__list--item--title')]/a/text()")).strip()
            house_url = "https://nb.lianjia.com" + "".join(
                div.xpath(".//p[contains(@class,'content__list--item--title')]/a/@href"))
            info = div.xpath(".//p[@class='content__list--item--des']//text()")
            local = "".join(info[1:4])
            if "/" in local:
                local = "".join(div.xpath(".//p[contains(@class,'content__list--item--brand')]/text()")).strip()
            area = info[6].strip()
            if "㎡" not in area:
                area = info[4].strip()
            orientation = info[8].strip()
            if "室" in orientation or "厅" in orientation:
                orientation = info[6].strip()
            home = info[-1].strip()
            if "室" not in home and "厅" not in home:
                home = info[10].strip()
            price = "".join(div.xpath(".//span[@class='content__list--item-price']//text()")).strip()
            label = "/".join(div.xpath(".//p[contains(@class,'content__list--item--bottom')]//text()")).strip()
            label = re.sub("\s", "", label)
            label = re.sub("//", "/", label)
        except:
            pass
  1. 保存数据到excel文件
f = xlwt.Workbook(encoding='utf_8')
sheet01 = f.add_sheet(u'sheet1', cell_overwrite_ok=True)
sheet01.write(0, 0, '标题')
sheet01.write(0, 1, '地区')
sheet01.write(0, 2, '面积')
sheet01.write(0, 3, '朝向')
sheet01.write(0, 4, '厅室')
sheet01.write(0, 5, '价格')
sheet01.write(0, 6, '标签')
sheet01.write(0, 7, '链接')


sheet01.write(num, 0, title)
sheet01.write(num, 1, local)
sheet01.write(num, 2, area)
sheet01.write(num, 3, orientation)
sheet01.write(num, 4, home)
sheet01.write(num, 5, price)
sheet01.write(num, 6, label)
sheet01.write(num, 7, house_url)

f.save('info' + '.xls')

完整代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值