网络爬虫笔记(Day5)——链家

本文介绍了一个针对链家租房信息的爬虫项目实践,包括URL构造、XPath定位及MySQL存储等关键技术环节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

注意:请不要爬取过多信息,仅供学习。

分析:

  1. 业务需求分析......(此例为住房信息...)
  2. 查找相关网页信息(以链家为例)
  3. 分析URL,查找我们需要的内容,建立连接
  4. 定位数据
  5. 存储数据

首先进入链家网首页,点击租房,F12检查网页,查找我们需要的信息。如图:

第一页url:https://bj.lianjia.com/zufang/

第二页url:https://bj.lianjia.com/zufang/pg2/

然后再定位我们需要的信息:如下图

 

下面就开始代码实现,我们的分析过程,获取数据,对数据进行定位。

主要代码:

# url 页码拼接
url = 'https://bj.lianjia.com/zufang/pg{}'.format(page)
# 利用Xpath 对数据进行定位
...
html_pipei = html_ele.xpath('//ul[@id="house-lst"]/li')

for pipei_one in html_pipei:
    title = pipei_one.xpath('./div[2]/h2/a')[0].text
    region = pipei_one.xpath('./div[2]/div[1]/div[1]/a/span')[0].text
    ...

完整代码如下:

import requests
from lxml import etree
import pymysql


class Mysql(object):
    '''执行数据操作封装类'''
    def __init__(self):
        '''连接数据库、创建游标'''
        self.db = pymysql.connect(host="localhost", user="root", password="8888", database="test")
        self.cursor = self.db.cursor()
        
    def mysql_op(self, sql, data):
        '''MySQL语句'''
        self.cursor.execute(sql, data)
        self.db.commit()
  
    def __del__(self):
        '''关闭游标、关闭数据库'''
        self.cursor.close()
        self.db.close()


# MySQL语句
Insert = Mysql()
# 要执行的sql 语句
sql = '''INSERT INTO lianjia (title, region, zone, meters, location, price) VALUES(%s, %s, %s, %s, %s, %s)'''

# 头部报文
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'
}


def download_msg():
    for page in range(1, 2):
        url = 'https://bj.lianjia.com/zufang/pg{}'.format(page)
        responses = requests.get(url, headers=headers)
        html = responses.text
        # 利用Xpath
        html_ele = etree.HTML(html)
        
        html_pipei = html_ele.xpath('//ul[@id="house-lst"]/li')
        # print(html_pipei)
        for pipei_one in html_pipei:
            # ./li/div[2]/a
            title = pipei_one.xpath('./div[2]/h2/a')[0].text
            # print(title)
            region = pipei_one.xpath('./div[2]/div[1]/div[1]/a/span')[0].text
            # print(region)
            zone = pipei_one.xpath('./div[2]/div[1]/div[1]/span[1]/span')[0].text
            # print(zone)
            meters = pipei_one.xpath('./div[2]/div[1]/div[1]/span[2]')[0].text
            # print(meters)
            location = pipei_one.xpath('./div[2]/div[1]/div[1]/span[3]')[0].text
            # print(location)
            price = pipei_one.xpath('.//div[@class="price"]/span')[0].text
            # print(price)
            data = (title, region, zone, meters, location, price)
            Insert.mysql_op(sql, data)
            

if __name__ == '__main__':
    download_msg()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值