简单Python爬取链接二手房信息

Python爬虫

博主目前学习Python爬虫,主要从简单的代码入手,然后逐步理解爬虫的精髓,逐渐学习复杂的爬虫技术等内容。

本文主要研究使用简单的线程、urllib、bs4.BeautifulSoup等爬取链家网站二手房信息。主要的原理在于链家网站具有比较简单的网页结构,同时也是静态网页;使用的手段,主要是通过网页之间的地址链接,不断获取房子详细页面的内容。

主要用到的包

本文主要使用以下package:

import Queue

import MysqlDB
import parserURL

import threading
import time
import urllib

from bs4 import BeautifulSoup

使用的线程信息,但由于存在链家可能存在反扒措施,或对同一IP存在访问数限制,因此这里主要使用单一线程。

主要代码

浏览页面网页解析:

# 解析网页信息
class parserURL(threading.Thread):

    counts = 0
    preurl = ""

    def __init__(self, html_queue, two_html_queue):
        threading.Thread.__init__(self)
        self.html_queue = html_queue
        self.two_html_queue = two_html_queue

    def run(self):
        while True:
            try:
                self.detail_url_parser()
                self.counts = self.counts + 1
                if self.counts % 100 == 0:
                    time.sleep(60)
            except Exception:
                self.html_queue.put(self.preurl)
                print "parserURL: Hello, world"

    def detail_url_parser(self):
        s = self.html_queue.get()
        print "parserURL: ", s
        self.preurl = s
        s = urllib.urlopen(s).read()
        s = BeautifulSoup(s, 'html.parser').find('ul', attrs={'class':'house-lst'})
        lis = s.find_all('li')

        for li in lis:
            detail = li.find('h2').find('a')
            detail_href = detail['href']
            self.two_html_queue.put("http://sh.lianjia.com" + detail_href)

解析房子详细页面信息,并将爬取内容写入数据库:

sql = "insert into house_infos(tag,type,total_price,house_type,house_area,house_avg_price,house_floor,house_year,house_desc,house_direction,first_pay,monthly_pay,district,district_address,city) values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
# 解析二手房信息
class parserErShouFang(threading.Thread):

    counts = 0
    preurl = ""

    def __init__(self, db, tag, city, house_type, html_queue):
        threading.Thread.__init__(self)
        self.html_queue = html_queue
        self.db = db
        self.tag = tag
        self.city = city
        self.house_type = house_type

    def run(self):
        while True:
            try:
                self.ershoufang_html()
                self.counts = self.counts + 1
                if self.counts % 500 == 0:
                    time.sleep(60)
            except Exception:
                print "parserErShowFang: Hello, world!"
                self.html_queue.put(self.preurl)

    def ershoufang_html(self):
        param = []

        h = self.html_queue.get()
        self.preurl = h
        print "sdf: ", h
        s = urllib.urlopen(h).read()
        s = BeautifulSoup(s, 'html.parser')

        param.append(self.tag)
        param.append(self.house_type)

        # 价格信息
        price = s.find('div', attrs={'class':'price'}).find('div', attrs={'class':'mainInfo bold'}).text
        param.append(price)
        # 户型信息
        huxing = s.find('div', attrs={'class':'room'}).find('div', attrs={'class':'mainInfo'}).text.replace(' ', '')
        param.append(huxing)
        # 面积信息
        area = s.find('div', attrs={'class':'area'}).find('div', attrs={'class':'mainInfo'}).text.replace(' ', '')
        param.append(area)

        lis = s.find('table', attrs={'class':'aroundInfo'}).find_all('td')
        for l in lis:
            p = l.text.replace('\t', '').replace('\r\n', '').replace(' ', '').replace('\n', '')
            tmp = p.split('\n')
            for q in tmp:
                pq = q.split(u'\uff1a')
                if pq[0].startswith(u'房源编号'):
                    break;
                if len(pq) > 1:
                    param.append(pq[1])
                    print pq[0], pq[1]
        param.append(self.city)

        self.db.execute(sql, param)

博主采用的手段比较低级,主要通过网页中的标签来获取相应的数据,后期可能通过xpath、正则表达式等方式来获取相应的内容。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值